gpt4 book ai didi

php mysql + session 问题

转载 作者:行者123 更新时间:2023-12-01 00:51:30 24 4
gpt4 key购买 nike

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

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

索引.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");

?>

最佳答案

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

关于php mysql + session 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com