会话保持到登录页面

时间:2014-09-28 12:11:25

标签: php mysql session

当我直接进入索引页面时,它会将我引导至登录页面。当我输入电子邮件和密码并提交时,它会保留在登录页面上。

我认为问题出在配置文件和索引页面之间的会话中。

这是配置:(请不要专注于mysql,我仍然想使用它)

<?php
ob_start();
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);
error_reporting(-1);
ini_set('display_errors', 'On');
mysql_connect("","","") or die("cannot connect");
mysql_select_db("") or die("Gagal");
$myemail= $_POST['myemail'];
$mypassword= $_POST['mypassword'];
$sql= "SELECT * FROM user WHERE myemail='".$myemail."' and mypassword='".$mypassword."'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
   {
    echo "Login successful";
    session_register("myemail");
    session_register("mypassword");
    header("location:index.php");
   }
else {
    echo "Wrong Username or Password";
    }
ob_end_flush();
?>

然后在索引页面中,我在标题中有这个会话:

<?php
session_start();
if(!session_is_resgitered(myemail)){
header("location:login.html);
}
?>

请帮我清除这个,我已经尝试了很多方法来实现这个登录功能。谢谢。

2 个答案:

答案 0 :(得分:0)

登录过程文件

<?php
session_start();
ob_start();

mysql_connect("localhost","root","") or die("cannot connect");
mysql_select_db("yourdatabase") or die("Gagal");

$myemail    = $_POST['myemail'];
$mypassword = $_POST['mypassword'];

$sql   = mysql_query("SELECT * FROM user WHERE email = '{$email}' AND password = '{$password}'");
$count = mysql_num_rows($sql);
if ( $count == 1 ) {
    $_SESSION['email']    = $myemail;
    $_SESSION['password'] = $mypassword;

    header("location: index.php");
} else {
    echo "Wrong Username or Password";
}
ob_end_flush();
?>

您的索引文件

<?php
session_start();

echo $_SESSION['email']; // try this first, if you make it right from the login page then it will give a value

// if ( $_SESSION['email'] == null && $_SESSION['password'] == null ) {
//    header("location:login.html");
// }
?>

答案 1 :(得分:0)

配置php中的

:你可以这样做。

if($count==1)
   {
    echo "Login successful";
    $_SESSION['user_loggedin']=$yourEmail; // this will create a session variable
    //session_register("myemail");
    //session_register("mypassword");
    header("location:index.php");
   }

并在index.php中

<?php
session_start();
$checkUser= $_SESSION['user_loggedin']; //here you can access the logged in user if it is logged in
//check the user
if(strlen($checkUser)){
    // user is logged in and access other details for current user
}else{
    // user is not logged in
}

?>