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 Answer 1

3

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

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.