0

我正在使用 php 和 mysql 创建一个简单的登录和注销脚本,但是当我尝试输入 login.php 或索引文件时,我收到一条错误消息:**页面未正确重定向 Firefox 已检测到服务器正在以永远不会完成的方式重定向对该地址的请求。

此问题有时可能是由禁用或拒绝接受 cookie 引起的。** 我不知道如何解决或错误是什么,如果有人帮助我,我将不胜感激

索引.php

<?php
require_once('connect.php');
ob_start();
session_start();
//checked wether the user is loged in  or not 

$user = $_SESSION['username'];

if(!isset($_SESSION['username']))
{
    $user = $_SESSION['username'];
    header("Location: index.php");
    exit();
}
else
{

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





// login script
if(isset($_POST['username'])&& isset($_POST['password']))
{
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']);
    $user_password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password']);
    $md5password = md5($user_password);
    $sql = mysql_query("SELECT id FROM members WHERE username = '".$user_login."' AND password = '".$user_password."'") or die ("could not select from database");

    $userCount = mysql_num_rows($sql);
    if($userCount ==1)
    {
        while($row = mysql_fetch_array($sql))
        {

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

        $_SESSION['id'] = $id;
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: index.php");
        exit();
    }
    else
    {
         echo "that info is incorrect";
         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>
<form action="login.php" method="post">

<input name="username" type="text" value="username" size="32" />
<input name="pass" type="password" value="password" size="32" />
<input name="login" type="submit" value="login" />

</form>

</body>
</html>
<?php  ob_end_flush(); ?>

主页.php

<?php
//home.php
session_start();
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{

    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";


}


?>

注销.php

<?php
session_start();
session_destroy();
header("Location: index.php");

?>
4

3 回答 3

2

index.php你需要把这个 if 条件放在'session_start();'之后

if($_SESSION['username'])
{
    header("Location: home.php");
    exit();
}

在 while 循环中,它应该header("Location: home.php");代替header("Location: index.php");

home.php页面中,您应该在打开 php 标签后放在顶部

ob_start();
session_start();

希望它会奏效。

++++++++++++++++++++++++++++++++++++++++++++++

使用此代码 index.php

<?php
require_once('connect.php');
ob_start();
session_start();
//checked wether the user is loged in  or not 

$user = $_SESSION['username'];

if($_SESSION['username'])
{
    $user = $_SESSION['username'];
    header("Location: home.php");
    exit();
}

// login script
if(isset($_POST['username'])&& isset($_POST['password']))
{
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']);
    $user_password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password']);
    $md5password = md5($user_password);
    $sql = mysql_query("SELECT id FROM members WHERE username = '".$user_login."' AND password = '".$user_password."'") or die ("could not select from database");

    $userCount = mysql_num_rows($sql);
    if($userCount ==1)
    {
        while($row = mysql_fetch_array($sql))
        {

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

        $_SESSION['id'] = $id;
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: home.php");
        exit();
    }
    else
    {
         echo "that info is incorrect";
         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>
<form action="login.php" method="post">

<input name="username" type="text" value="username" size="32" />
<input name="pass" type="password" value="password" size="32" />
<input name="login" type="submit" value="login" />

</form>

</body>
</html>
<?php  ob_end_flush(); ?>

主页.php

<?php
ob_start();
session_start();

//home.php
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{

    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";


}
?>

logout.php是正确的

于 2013-03-07T15:07:39.337 回答
2

首先,在 index.php 中你不需要“//检查用户是否登录”,我们应该在 home.php 中检查。此代码导致您的错误:“页面未正确重定向 Firefox 检测到服务器正在以永远不会完成的方式重定向此地址的请求”。您进行了重复(未创建会话,但已检查...)。

其次,在 home.php 中,你必须编写 session_start() 方法,这是使用 session 时需要的代码。

参考我的代码:

索引.php

<?php
ob_start();
session_start();

//check session is existed    
if (isset($_SESSION['username'])) {
    header("Location: home.php");
}

if (isset($_POST['username']) && isset($_POST['password'])) {
    $user_login = $_POST['username'];
    $user_password = $_POST['password'];

    if ($user_login == 'namluu' && $user_password =='123456') {
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: home.php");
        exit();
    } else {
        echo 'Infor not correct';
        exit();
    }
}
?>
<html>
  <head></head>
  <body>
    <form action="index.php" method="post">
      <input type="text" name="username" />
      <input type="text" name="password" />
      <input type="submit" name="login" value="login" />
    </form>
  </body>
</html>
<?php
  ob_end_flush();
?>

主页.php

<?php
session_start();
//home.php
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{
    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";
}
?>
于 2013-03-07T15:34:25.360 回答
1

您还没有到达session_start()home.php 的顶部,这意味着您将在 home.php 和 index.php 之间创建一个无限循环。

目前正在发生的事情是当您访问 index.php 时,它会识别会话并将用户重定向到 home.php。由于session_start()home.php 中没有,它无法识别会话并将用户重定向回 index.php。因此,您有一个无限循环。

于 2013-03-07T15:00:48.310 回答