0

我正在使用会话创建一个简单的登录表单,但问题是当我按下登录时它会将我重定向到index.php 但我需要转到home.php。在logout.php中,我破坏了会话并重定向到index.php,但不知何故,登录按钮将我重定向到index.php , 就像在登录过程中没有成功一样,我非常需要如何修复这个错误。

索引.php

<?php
require_once('global.php');

if(@$logged == 1)
{

    header("Location: home.php");
    exit();
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index page</title>
</head>

<body>
<h1> this is the index page</h1>
<a href="login.php">Login</a>
</body>
</html>

全局.php

<?php
session_start();
 require_once('connect.php'); 

// cheking if the sessions are set

if(isset($_SESSION['username']))
{
    $session_username = $_SESSION['username'];
    $session_pass = $_SESSION['password'];
    $session_id = $_SESSION['id'];

    //cheking if the member exist

    $query = mysql_query("SELECT * FROM members WHERE id = '".$session_id."' AND password = '".$session_pass."' LIMIT 1") or die("could not select memeber");
    $count_count = mysql_num_rows($query);
    if($count_count > 0)
    {
      $logged = 1;
      while($row = mysql_fetch_array($query))
      {
          $session_username = $row['username'];
      }
      $_SESSION['username'] = $session_username;
      $_SESSION['pass'] = $session_pass;
      $_SESSION['id'] = $session_id;        

    }
    else
    {
        header("Location: logout.php");
        exit();
    }
}
else
{
    // if the user not loged in
    $logged = 0;

}

?>

登录.php

<?php
require_once('global.php');


$message = "";
if(isset($_POST['email']))
{
    $email = $_POST['email'];
    $pass = $_POST['password'];

    // error handling
    if((!$email) ||(!$pass))
    {
        $message = 'please insert both fields';

    }
    else
    {
        //secure data
        $email = mysql_real_escape_string($email);
        $pass = sha1($pass);
        $query = mysql_query("SELECT * FROM members WHERE email = '".$email."' AND password = '".$pass."'LIMIT 1") or die("could not select data");
        $count_query = mysql_num_rows($query);
        if($count_query == 0)
        {
            $message = 'your info was inccorrect';
        }
        else
        {
            //start SESSIONS
            $_SESSION['pass'] = $pass;
            while($row = mysql_fetch_array($query))
            {

                $username = $row['username'];
                $id = $row['id'];
            }
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $id;

        }
        header("Location: home.php");
    }





}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login to membership website </title>
</head>

<body>
<h1> login to my website</h1>
<p><?php print("$message"); ?></p>
<form action="login.php" method="post">
<input type="text" name="email" placeholder="email adress" /><br />
<input type="password" name="password" placeholder="password" /><br />

<input type="submit" value="Login" />
</form>


</body>
</html>

主页.php

<?php
 require_once('global.php');
if($logged == 0)
{

    header("Location: index.php");
    exit();
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1>this the home page</h1>
</body>
</html>

注销.php

<?php 
session_start();

session_destroy();
/*
if(session_is_registered('username'))
{

    echo "you are loged in we can not log you out"; 
    exit();

}
*/
//else
//{

    header("Location: index.php");
//}


?>
4

2 回答 2

0

当您使用 $_SESSION['username'] 检查会话时,您不需要记录的变量。您可以允许用户在 $_SESSION['username'] 存在时访问该页面,并且如果它没有将他重定向到登录页面

于 2013-03-04T08:24:48.100 回答
0

老实说,这是相当意大利面编码的,有点乱,但问题是 login.php 没有设置 $logged = true 所以 login.php 重定向到 home.php 然后 home.php 重定向到 index.php

所以试试这个

登录.php

<?php
require_once('global.php');

$message = "";
if(isset($_POST['email'])) {
    $email = $_POST['email'];
    $pass = $_POST['password'];

    // error handling
    if((!$email) ||(!$pass)) {
        $message = 'please insert both fields';
    }
    else
    {
        //secure data
        $email = mysql_real_escape_string($email);
        $pass = sha1($pass);
        $query = mysql_query("SELECT * FROM members WHERE email = '".$email."' AND password = '".$pass."'LIMIT 1") or die("could not select data");
        $count_query = mysql_num_rows($query);
        if($count_query == 0) {
            $message = 'your info was inccorrect';
        } else {
            //start SESSIONS
            $_SESSION['pass'] = $pass;
            while($row = mysql_fetch_array($query)) {
                $username = $row['username'];
                $id = $row['id'];
            }
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $id;
            // NEW LINE
            $logged = 1;
        }
        header("Location: home.php");
    }
}
?>
于 2013-05-30T22:02:30.097 回答