gpt4 book ai didi

javascript - PHP登录脚本重定向在登录后不起作用

转载 作者:行者123 更新时间:2023-11-29 12:21:52 25 4
gpt4 key购买 nike

我发现并修复了一点,我不太擅长 PHP,但欢迎任何改进。

问题在于,有时在 Chrome 和 Opera 中,但仅有时在登录成功后,脚本会在 5 秒后使用 javascript 重定向重定向到欢迎页面。但有时它只是卡住并且不重定向,而只是显示一个没有错误的白页,而其他时候它会重定向并且工作正常。它可能是什么?

这是代码

<?php session_start();?>
<?php
include 'inc/connection.php';
$db=mysqli_connect($dbserver, $dbuser, $dbpass, $dbname)or die("DB connection error...");

$username_to_sanitize = $_POST['username'];
$password_to_sanitize = $_POST['password'];
$sanitized_username = mysqli_real_escape_string($db, $username_to_sanitize);
$sanitized_password = mysqli_real_escape_string($db, $password_to_sanitize);

$query = "SELECT password, salt, privilege, username FROM members WHERE username = '$sanitize_username'";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 0) // User not found. Redirected to login page.
{header('Location:login.php?message=Username not found, please try again');}

$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $sanitized_password) );

if($hash != $userData['password']) // Incorrect passw. Redirected to login page.
{header('Location:error.php?message=Wrong password, please try again');}

else if($userData['privilege']=="ADMIN"){session_start();
$_SESSION['username']=$userData['username'];
header('Location:redirection.php?URL=admins/index.php');}

else if($userData['privilege']=="MODERATOR"){session_start();
$_SESSION['username']=$userData['username'];
header('Location:redirection.php?URL=moderators/index.php');}

else if($userData['privilege']=="MEMBER"){session_start();
$_SESSION['username']=$userData['username'];
header('Location:redirection.php?URL=members/index.php');}

else if($userData['privilegio']=="BANNED"){session_start();
$_SESSION['username']=$userData['username'];
header('Location:redirection.php?URL=banned/index.php');}

else{
header('Location:error.php?message=su need privileges to acces this site');
exit();
}
?>

阅读并测试在互联网上找到的新脚本后,两个月后我仍然无法解决这个问题。有什么想法吗?

最佳答案

你的代码中有很多重复,这很糟糕,因为你重复的每个地方都意味着你更新代码时需要更改它,这意味着以后有更多的地方会出现错误。

为了提供帮助,我只放置了一个 session_start() ,我转换了if/elseif/elseif/elseif...到 switch 语句。

我没有处理位置 header 本身,而是将其替换为 http_redirect function ,这基本上可以为您完成。启动时,它会为您编码 URL,因此您不必担心这一点。

如果您一直看到空白页面,那么您应该检查网络服务器的日志(apache 或 nginx 或 php-fpm 或其他)以查看是否存在错误。否则,打开更好的错误报告;通常空白页只是尚未报告的错误。

<?php 
session_start();
include 'inc/connection.php';
$db = mysqli_connect($dbserver, $dbuser, $dbpass, $dbname) or die('DB connection error...');

$sanitized_username = mysqli_real_escape_string($db, $_POST['username']);
$sanitized_password = mysqli_real_escape_string($db, $_POST['password']);

$query = "SELECT password, salt, privilege, username FROM members WHERE username = '$sanitized_username'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) == 0) {
// User not found. Redirected to login page.
http_redirect('login.php', array('message' => 'Username not found, please try again'), true);
}

$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $sanitized_password) );

if($hash != $userData['password']) {
// Incorrect passw. Redirected to login page.
http_redirect('error.php', array('message' => 'Wrong password, please try again'), true);
}
// Just set the username once
$_SESSION['username'] = $userData['username'];

switch ( $userData['privilege'] ) :
case 'ADMIN':
http_redirect('redirection.php', array('URL' => 'admins/index.php'), true);
break;
case 'MODERATOR' :
http_redirect('redirection.php', array('URL' => 'moderators/index.php'), true);
break;
case 'MEMBER' :
http_redirect('redirection.php', array('URL' => 'members/index.php'), true);
break;
case 'BANNED' :
http_redirect('redirection.php', array('URL' => 'banned/index.php'), true);
break;
default:
// The message is weird. Should it be:
// 'You need privileges to access this site' or something like that?
http_redirect('error.php', array('message' => 'su need privileges to acces this site'), true);
break;
endswitch;
http_redirect('error.php', array('message' => 'su need privileges to acces this site'), true);
?>

关于javascript - PHP登录脚本重定向在登录后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884699/

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