I have been learning PHP and MySQL for some time now and I encountered a problem. I have created a user database (MySQL), login form and page where you are supposted to be redirected after succesful login.
Login page
<?php
require_once 'login.php'; //connecting to db
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
session_start(); //
if (isset($_POST['username']) &&
isset($_POST['password']))
{
$username = $_POST["username"];
$password = $_POST["password"];
$salt1 = "qm&h*";
$salt2 = "pg!@";
$query = "SELECT * FROM login WHERE username='$username'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) die("Wrong username or password.");
elseif (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
$crypt_login_pass = md5("$salt1$password$salt2");
if ($crypt_login_pass == $row[2])
{
$_SESSION['username'] = $username;
$_SESSION['password'] = $crypt_pass;
header("Location: index_account.php");
exit;
}
else {
die("Wrong username or password.");
}
}
}
?>
The code ran flawlessly until I added the header("Location: index_account.php")
. I used to have there just die("You are now logged in.")
.
And here is the page you are supposted to be redirected to:
<?php
session_start();
if (isset($_SESSION['username']) &&
isset($_SESSION['password']))
{
$username = $_SESSION['username'];
$password = $_SESSION['password'];
echo "Your username is '$username'
and your crypted password is '$password'.";
}
else echo "Please <a href=index.php>click here</a> to log in.";
?>
All this code is running on localhost. The problem is that the $_SESSION['username'] and $_SESSION['password'] won't transfer to the second page (index_account.php). I have read many post and the most similiar I've found is this: Session variables lost after header redirect but without answear. Note that this is my first programing language (excluding HTML and CSS) and I am still very new to this.
P.S. Sorry for my Enlish. It's not my native language.
Thanks in advance!