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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.