bravo14 Posted September 21, 2008 Share Posted September 21, 2008 Hi all I am starting to work with sessions (first attempt), the user signs in using a the following code $email=$_POST["email"]; $password=$_POST["password"]; //select database mysql_select_db('phonestore', $con); $query = "SELECT * FROM customers WHERE email = '".$email."'"; $result=(mysql_query($query,$con)); if (!mysql_query($query,$con)){ die('Error: ' . mysql_error()); } else { // count the number of rows $no_of_results = mysql_num_rows($result); } // logic test - if there is 1 or more row in the query result output an message that the email address already exists if(mysql_num_rows($result) > 0) { $sql="SELECT * FROM customers WHERE email ='$email' and password=md5('$password')"; $result2=(mysql_query($query,$con)); if (!mysql_query($query,$con)){ die('Error: ' . mysql_error()); } else { echo // count the number of rows $no_of_results2 = mysql_num_rows($result2); $_SESSION['auth']="yes"; $_SESSION['logname']=$email; header("Location: account.php"); } } mysql_close($con); ?> and then go to page account.php in the following code <?php session_start(); if($_SESSION['auth'] !="yes") { header ("Location:login_reg.php"); } else { echo"<html><head><title>Your Account</title></head><body>"; echo"<h2>Welcome back" .$_SESSION['name']."</h2></body></html>"; } ?> The problem I have is that even after I log on it takes me back to the login page Any ideas or advice would be much appreciated Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/ Share on other sites More sharing options...
Fadion Posted September 21, 2008 Share Posted September 21, 2008 The problem is that you haven't started the session variables with a session_start() call in your login page. You've called it in account.php, but not in the login page. Each page that uses session variables, must have a call to session_start() to initialize them. Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/#findComment-646969 Share on other sites More sharing options...
bravo14 Posted September 21, 2008 Author Share Posted September 21, 2008 Thanks for your reply, do I start it after I set the variable or before? Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/#findComment-646984 Share on other sites More sharing options...
peranha Posted September 21, 2008 Share Posted September 21, 2008 You start it at the beginning of the page as you did on the account.php page. Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/#findComment-646985 Share on other sites More sharing options...
bravo14 Posted September 21, 2008 Author Share Posted September 21, 2008 Cheers Guys as ever the phonts of all knowledge, one last thing is this bit correct to be able to display the user details ? <?php session_start(); if($_SESSION['auth'] !="yes") { header ("Location:login_reg.php"); } else { echo"<html><head><title>Your Account</title></head><body>"; echo"<h2>Welcome back "$_SESSION['email']."</h2></body></html>"; } ?> For now I just want to display the email from the previous page. Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/#findComment-646989 Share on other sites More sharing options...
peranha Posted September 21, 2008 Share Posted September 21, 2008 echo"<h2>Welcome back "$_SESSION['email']."</h2></body></html>"; should be echo"<h2>Welcome back ".$_SESSION['email']."</h2></body></html>"; you forgot the . before $_SESSION Link to comment https://forums.phpfreaks.com/topic/125168-php-sessions/#findComment-646992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.