gpt4 book ai didi

php 和 session 重定向错误

转载 作者:行者123 更新时间:2023-11-29 03:40:04 24 4
gpt4 key购买 nike

我正在创建一个登录表单并使用 session 注销但是当我尝试登录浏览器时显示错误(我认为错误在记录的函数中) 任何人都可以帮助我,因为它让我发疯.....**页面没有正确重定向 Firefox 让服务器以一种永远不会检测到完整的方式重定向对此地址的请求。

这个问题有时可能是禁用或拒绝接受导致的 cookies 。**

登录.php

<?php
ob_start();
require_once('functions.php');


if(loggedin()){
header("Location: userarea.php");
exit();
}


if(isset($_POST['login'])){

//get data
$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if($username && $password){

$login = mysql_query("SELECT * FROM rememberme WHERE username = '$username'");
while($row = mysql_fetch_assoc($login)){

$db_password = $row['password'];
if($password == $db_password)
$loginok = TRUE;
else
$loginok = FALSE;
if($loginok == TRUE){

if($rememberme == "on"){
setcookie("username", $username, time()+7200);



}else if($rememberme == "")
$_SESSION['username'] = $username;
header("Location: userarea.php");
exit();


}else{
die("incorrect username/password combination");

}

}



}else
die("please enter a username and password");
exit();


}


?>

<form action="login.php" method="post">
<p>Username<br />
<input type="text" name="username" />
</p>
<p> Password<br />
<input type="password" name="password" />
</p>
<p>
<input type="checkbox" name="rememberme">
Remember me<br />

<input type="submit" name="login" value="Log in" />
</p>
</form>
<?php ob_flush(); ?>

函数.php

 <?php
//session
session_start();
// connect to database
mysql_connect("localhost", "root", "") or die("could not connect to database");
mysql_select_db("rememberme") or die("could not select database");

// login check function

function loggedin()
{
$loggedin = false;
if(isset($_SESSION['username'])||isset($_COOKIE['username']))
{
$loggedin = TRUE;
}
return $loggedin;
}

?>

用户区.php

<?php
//session
session_start();
// connect to database
mysql_connect("localhost", "root", "") or die("could not connect to database");
mysql_select_db("rememberme") or die("could not select database");

// login check function

function loggedin()
{

if(isset($_SESSION['username'])||isset($_COOKIE['username'])){
$loggedin = TRUE;
return $loggedin;


}



}

?>

最佳答案

你在两个文件中有相同的功能。

function loggedin()

尝试

function loggedin()
{
$loggedin = false;
if(isset($_SESSION['username'])||isset($_COOKIE['username']))
{
$loggedin = TRUE;
}
return $loggedin;
}

如果你 if 条件不满足你的函数将不会返回 true 或 false,所以你需要将 initial 值设置为 false,然后如果 if condition 满足,则覆盖它。

登录.php

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

if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if($username && $password)
{
$login = mysql_query("SELECT * FROM rememberme WHERE username = '".$username."'");
while($row = mysql_fetch_assoc($login))
{
$db_password = $row['password'];
if($password == $db_password)
$loginok = TRUE;
else
$loginok = FALSE;
if($loginok == TRUE)
{
if($rememberme == "on")
{
setcookie("username", $username, time()+7200);
}
else if($rememberme == "")
{
$_SESSION['username'] = $username;
header("Location: userarea.php");
exit();
}
}
else
{
die("incorrect username/password combination");
}

}
}
else
{
die("please enter a username and password");
exit();
}
}
?>

<form action="login.php" method="post">
<p>Username<br />
<input type="text" name="username" />
</p>
<p>Password<br />
<input type="password" name="password" />
</p>
<p>
<input type="checkbox" name="rememberme">
Remember me<br />
<input type="submit" name="login" value="Log in" />
</p>
</form>
<?php ob_flush(); ?>

函数.php

<?php
ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');

mysql_connect("localhost", "root", "") or die("could not connect to database");
mysql_select_db("rememberme") or die("could not select database");

// login check function

if(loggedin())
{
header("Location: userarea.php");
exit();
}

function loggedin()
{
$loggedin = false;
if(isset($_SESSION['username'])||isset($_COOKIE['username']))
{
$loggedin = TRUE;
}
return $loggedin;
}

?>

userarea.php

<?php
require_once "functions.php";
echo "after login";
?>

关于php 和 session 重定向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086333/

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