0

This a Login Form from where we redirect to login_success.php page when username/password match with database values.

Checklogin.php

<?php
include 'config.php';

// username and password sent from form
$myusername=$_POST['email'];
$mypassword=$_POST['password'];


// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM drsignup WHERE email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['email']=$myusername;
$_SESSION['password']=$mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

Login_success.php

This form shows a Welcome message for the user who logs in and session starts.In below code, I am not able to get $_SESSION['email'] value with Welcome string and shows undefined index email.

I tried to collect session_id from session_id(); function which gives value like e9ubrn9v239o60tfalag6e6kt4. Please let me know why i am not able to get email id here.How can i store my session variables on my all php page.

<?php
session_start();

echo  session_id();
SESSION_WRITE_CLOSE();

if(isset ($_SESSION['email']))
{
echo "Welcome $_SESSION['email']";
}
else
{
header("location:Drlogin.php");
}
?>
4
  • 1
    Where is your session_start() on the first page? Commented Jul 27, 2013 at 13:16
  • Add session_start() to config file. Commented Jul 27, 2013 at 13:18
  • 1
    possible duplicate of PHP session variables not carrying over to my logged in page, but session ID is Commented Jul 27, 2013 at 13:19
  • 1
    Just a quick note, why do you save the password in the session? Wouldn't recommend that. Commented Jul 27, 2013 at 14:20

1 Answer 1

3

You need to add session_start() in Checklogin.php. You need to start your session on every page that uses session.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.