【问题标题】:php mysql + session problemsphp mysql + 会话问题
【发布时间】:2013-03-07 14:55:30
【问题描述】:

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

这个问题有时可能是由于禁用或拒绝接受 饼干。** 如果有人帮助我,我不知道如何解决或错误是什么,我将不胜感激

index.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(); ?>

home.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>";


}


?>

logout.php

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

?>

【问题讨论】:

  • 也许不是一个解决方案,但是在 index.php 中,您在获得它的值之后检查是否设置了 $_SESSION['username']。如果未设置,则尝试再次从 $_SESSION['username'] 获取值。
  • 您在 SQL 查询中连接用户名和密码。小心 SQL 注入。
  • @ ThaMe90 如果我删除 if 语句中的 $_SESSION['username']** 我会得到相同的错误消息,但如果删除 **$_SESSION['username'] 我收到一个错误,指出索引用户名未定义,但现在很奇怪,在这两种情况下我都收到相同的错误消息
  • @LION 这是一个简单的例子,但即使在这一小块代码中我也会遇到错误,所以如果有人知道如何解决它,请给我帮助

标签: php mysql session login dreamweaver


【解决方案1】:

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(); ?>

home.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 是正确的

【讨论】:

  • +1 我来的有点晚了,但这个答案几乎就是我要说的。
【解决方案2】:

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

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

参考我的代码:

index.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();
?>

home.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>";
}
?>

【讨论】:

  • 它应该在index.php 上使用检查$_SESSION['username']。如果此会话变量存在该值,则用户不得进入索引页面。它应该在主页上重定向。用户必须在注销后登录index.php
  • @Kabir :哦,你说得对!我忘记了。但是oigin代码有问题。我刚刚编辑了零件检查会话。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多