It doesn't work and I've spent hours and half my hairs trying to work out why. I already had the form imbedded in my html index file with this code:
I want to create login and logout session
My table in mysql database is looking like this
CREATE TABLE member ( id int(10) NOT NULL auto_increment, userName varchar(50) NOT NULL, passWord varchar(50) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM utf8_general_ci AUTO_INCREMENT=3 ;
<?php
// Inialize session
session_start();
// Include database connection settings
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'database'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
// Retrieve username and password from database according to user's input
$userName=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$passWord=md5($password); // Encrypted Password
//*********retrieving data from Database**********
$query = "SELECT * FROM member WHERE userName='$userName' and passWord='$passWord'";
//$login = mysqli_query("SELECT userName,password FROM 'member' WHERE userName= $_post['username'] AND passWord= $_post['password'])");
// Check username and password match
$res = mysql_query($query);
$rows = mysql_num_rows($res);
if ($rows==1) {
// Set username session variable
$_SESSION['userName'] = $_POST['username'];
// Jump to secured page
header("Location: securedpage.php");
}
else {
// Jump to login page
echo "user name and password not found";
}
exit;
?>
in this code while login it goes directly to user name and password not found even username and password is correct