How to fix session variable problem?

I’m making a member based website.
When user get register, the data of user goes into
MySQl tables (users, profile and images).
Now I have a php file functions.php to connect database.
I included this file in the start of every page.

functions.php code is here

<?php session_start(); $email = isset($_SESSION['email']); mysql_connect("localhost","root","") or die(); mysql_select_db("mydatabase") or die(); //login check function function loggedin() { if (isset($_SESSION ['email'])||isset($_COOKIE['email'])) { $loggedin = TRUE; return $loggedin; } } ?>

Now for login check I have login.php file.

login.php code is here.

<?php include 'functions.php'; if (loggedin()) { header("Location: userarea.php"); exit(); } if ($_POST['submit']) { //get data $email = $_POST['email']; $password = $_POST['password']; $rememberme = isset ($_POST['rememberme']); if ($email&&$password) { $login = mysql_query("SELECT * FROM users WHERE email='$email' "); while($row = mysql_fetch_assoc($login)) { $db_password = $row['password']; if(md5($password)==$db_password) $loginok = TRUE; else $loginok = FALSE; if ($loginok==TRUE) { if ($rememberme=="on") setcookie("email",$email, time()+7200); else if ($rememberme=="") $_SESSION['email']=$email; header("Location: userarea.php"); exit(); } else die("Incorrect Email/Password combination."); } } else die("Please enter an Email and Password"); } ?>

As you can see that if user logged in, he’ll redirected to userarea.php page.

userarea.php code is here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-… <?php include 'functions.php'; if (!loggedin()) { header("Location: index.php"); exit(); } $error = "Unable to Connect."; $connect = mysql_connect("localhost","root","") or die ($error); mysql_select_db("mydatabase") or die ($error); $email = isset($_SESSION['email']); echo $email; //doesn't echo any thing. $query = mysql_query(" SELECT * FROM images WHERE email = '$email' "); if(mysql_num_rows($query)==0) die ("User not found!"); else { $row = mysql_fetch_assoc($query); $location = $row['imagelocation']; echo "";

$result = mysql_query(“SELECT * FROM profile
WHERE email=’$email”);

while($row = mysql_fetch_array($result))
{
echo $row[‘firstname’] . " " . $row[‘lastname’];
echo "
";
}

}

?>

When user get register, tables take data perfectly.
When user get login, it also works pretty good but
on userarea.php when I try to echo image from
images table and firstname and lastname from profile
table, it says user not found while I there is user’s data
in tables. But when I change $email variable with the email of user, it retrieves data nicely
like

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-… <?php include 'functions.php'; if (!loggedin()) { header("Location: index.php"); exit(); } $error = "Unable to Connect."; $connect = mysql_connect("localhost","root","") or die ($error); mysql_select_db("mydatabase") or die ($error); $email = isset($_SESSION['email']); echo $email; //doesn't echo any thing. $query = mysql_query(" SELECT * FROM images WHERE email = '[email protected]' "); if(mysql_num_rows($query)==0) die ("User not found!"); else { $row = mysql_fetch_assoc($query); $location = $row['imagelocation']; echo "";

$result = mysql_query(“SELECT * FROM profile
WHERE email='[email protected]”);

while($row = mysql_fetch_array($result))
{
echo $row[‘firstname’] . " " . $row[‘lastname’];
echo "
";
}

}

?>

I don’t know where is the problem either in $email variable
or somewhere in session.
Tell me about it please.

Did you try starting the session again in userarea.php?
before this:
[php]
$email = isset($_SESSION[‘email’]);
echo $email; //doesn’t echo any thing.
[/php]

then remove the isset.
[php]
session_start();
$email = $_SESSION[‘email’];
echo $email; //doesn’t echo any thing.
[/php]

I tried as you suggest me. it says

“Notice: A session had already been started - ignoring session_start() in C:\wamp\www\userarea.php on line 66”

Because I have included “functions.php” at start of this page and in “functions.php” i sarted a session already.

Hi Shani,

Try to include your inclusions once, as:

 include_once('functions.php'); 

i tried
[php]include_once(‘functions.php’); [/php]
it’s not working.
I don’t know what happened, it was working pretty good some days before. I didn’t change any code from functions.php or from userarea.php.

Sponsor our Newsletter | Privacy Policy | Terms of Service