I am having a problem with the $_SESSION
function in my php website. I set the function and then try to access it in another page, but it just comes up as blank.
This is the file login.php
where I set the $_SESSION
<?php
session_start();
?>
<html>
<?php
$email = $_POST['email'];
$password = $_POST['password'];
$con = mysqli_connect("host","user","pass","db");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
if ($email && $password){
$result = mysqli_query($con, "SELECT * FROM users WHERE email='$email'");
$numrows = mysqli_num_rows($result);
if ($numrows != 0){
//username exists
$row = mysqli_fetch_row($result);
if ($password != $row[2]){
print 'Incorrect password<br />';
$error = TRUE;
}
else {
//password is correct
echo "You're in! <a href='member.php'>Click</a> here to enter member page";
$_SESSION['email'] = $email;
}
}
else {
//email not found
print 'Sorry, that email cannot be found.<br />';
$error = TRUE;
}
}
else {
die('Please enter an email and password<br />');
$error = TRUE;
}
if ($error){
print '<a href=index.php>Go Back</a>';
}
?>
</html>
And this is the member.php
file.
<?php
session_start();
if (isset($_SESSION['email'])){
$username = $_SESSION['email'];
echo "Your email is ";
echo $username;
}
?>
When I press the link to go the the member.php
page via the link, the page is completely blank, presumably suggesting that the $_SESSION['email']
has nothing in it.
Please let me know where I am going wrong, and how to rectify the issue.
Any help would be much appreciated.
If anyone wants to see how the site is actually working, please go here.
if (isset($_SESSION['username'])){
should most likely beif (isset($_SESSION['email'])){
---$_SESSION['username']
isn't defined anywhere, until you do define it. Otherwise, get rid of it. – Funk Forty Niner Mar 17 '14 at 18:55$_SESSION['email'] = $email;
to$_SESSION['email'] = $username;
since you're checking with$username = $_SESSION['email'];
– Funk Forty Niner Mar 17 '14 at 18:59var_dump();
then. – Funk Forty Niner Mar 17 '14 at 19:03