Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysql+;会话问题_Php_Mysql_Session_Login_Dreamweaver - Fatal编程技术网

php mysql+;会话问题

php mysql+;会话问题,php,mysql,session,login,dreamweaver,Php,Mysql,Session,Login,Dreamweaver,我正在使用php和mysql创建一个简单的登录和注销脚本,但当我尝试输入login.php或索引文件时,会收到一条错误消息,上面说: **页面未正确重定向 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求 此问题有时可能是由于禁用或拒绝接受导致的 饼干** 我不知道如何解决或是什么错误,如果有人帮助我,我将不胜感激 index.php home.php顶部没有session_start(),这意味着您将在home.php和index.php之间创建一个无限循环 当前的情

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

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

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


当前的情况是,当您访问index.php时,它会识别会话并将用户重定向到home.php。由于home.php中没有
session\u start()
,因此它无法识别该会话并将用户重定向回index.php。因此,您有一个无限循环。

home.php顶部没有
session\u start()
,这意味着您将在home.php和index.php之间创建一个无限循环


当前的情况是,当您访问index.php时,它会识别会话并将用户重定向到home.php。由于home.php中没有
session\u start()
,因此它无法识别该会话并将用户重定向回index.php。因此,您有一个无限循环。

index.php
中,您需要在“session_start();”之后将此if条件置于顶部

在while循环中,它应该是
标题(“Location:home.php”)
而不是
标题(“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
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>";


}
?>
<?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
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_start();”之后将此if条件置于顶部

在while循环中,它应该是
标题(“Location:home.php”)
而不是
标题(“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
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>";


}
?>
<?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
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中不需要“//检查用户是否登录”,我们应该在home.php中检查。
这段代码导致了您的错误:“页面没有正确重定向Firefox检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求”。您进行了重复(未创建会话,但已选中…)

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

请参阅我的代码:

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


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

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


}
?>
<?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
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中不需要“//检查用户是否登录”,我们应该在home.php中检查。 这段代码导致了您的错误:“页面没有正确重定向Firefox检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求”。您进行了重复(未创建会话,但已选中…)

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

请参阅我的代码:

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


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

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


}
?>
<?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
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中,在获取其值后立即检查是否设置了
$\u会话['username']
。如果未设置,则尝试再次从
$\u会话['username']
获取该值。您将在SQL查询中连接用户名和密码。注意SQL注入。@ThaMe90如果我在if语句中删除$$\u会话['username']**我将得到相同的错误消息,但是如果删除**$\u会话['username']我得到一个错误,说索引用户名是未定义的,但现在奇怪的是,我在两种情况下都得到了相同的错误msg@LION这是一个简单的示例,但即使在这一小块代码中,我也会遇到一个错误,因此如果有人知道如何解决它,请给我一个帮助,也许不是解决方案,但在index.php中,您可以检查是否设置了
$\u SESSION['username']
,在你得到它的价值之后。如果未设置,则尝试再次从
$\u会话['username']
获取该值。您将在SQL查询中连接用户名和密码。注意SQL注入。@ThaMe90如果我在if语句中删除$$\u会话['username']**我将得到相同的错误消息,但是如果删除**$\u会话['username']我得到一个错误,说索引用户名是未定义的,但现在奇怪的是,我在两种情况下都得到了相同的错误msg@LION这是一个简单的例子,但即使在这一小块代码中,我也会遇到一个错误,因此如果有人知道如何解决它,请给我一个帮助,它应该在
index.php
上使用check
$\u SESSION['username']
。如果此会话变量存在该值,则用户不得出现在索引页上。它应该在主页上重定向。用户必须在注销后启动
index.php
。@Kabir:哦,你说得对!我忘了。但oigin代码有问题。我刚刚编辑了部件检查会话。它应该在
index.php
上使用检查
$\u会话['username']
。如果此会话变量存在该值,则用户不得出现在索引页上。它应该在主页上重定向。用户必须在注销后启动
index.php
。@Kabir:哦,你说得对!我忘了。但oigin代码有问题。我刚刚编辑了零件检查部分。+1我来得有点晚,但这个答案几乎正是我要说的。+1我来得有点晚,但这个答案几乎正是我想要的