Jump to content

PHP Sessions


bravo14

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.