1

我希望有人可以帮助我处理我的登录脚本。当我从 wamp localhost 运行它时,该代码运行良好,但是当它在服务器上运行时,它在我尝试登录时返回一个空白页面,谁能告诉我为什么?代码如下。

<?php
session_start();

//login script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'login'){

//give your login credentials here
if($_REQUEST['uname'] == 'zipbuzz' && $_REQUEST['pass'] == 'sewilt')
$_SESSION['login_user'] = 1;
else
$_SESSION['login_msg'] = 1;
}

//get the page name where to redirect
if(isset($_REQUEST['pagename']))
$pagename = $_REQUEST['pagename'];

//logout script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'logout'){
unset($_SESSION['login_user']);
header('Location:login.php');
}
if(isset($_SESSION['login_user'])){
if(isset($_REQUEST['pagename']))
header('Location:'.$pagename.'.php');
else
header('Location:admin.php');
}else{
?>


<?php
//display the error msg if the login credentials are wrong!
if(isset($_SESSION['login_msg'])){
echo 'Wrong username and password !';
unset($_SESSION['login_msg']);
}
?>

然后我将以下内容添加到需要登录才能访问的页面中。

<?php session_start();
//check logged in or not!
if(!isset($_SESSION['login_user'])){
header('Location:login.php?pagename='.basename($_SERVER['PHP_SELF'], ".php"));
}
?>
4

2 回答 2

0

你有else{ ?>那个没有关闭。

于 2013-08-19T21:10:51.553 回答
0

首先,这是您发布的代码的更正版本。

<?php
session_start();
//login script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'login')
{
    //give your login credentials here
    if($_REQUEST['uname'] == 'zipbuzz' && $_REQUEST['pass'] == 'sewilt')
        $_SESSION['login_user'] = 1;
    else
        $_SESSION['login_msg'] = 1;
}

//get the page name where to redirect
if(isset($_REQUEST['pagename']))
    $pagename = $_REQUEST['pagename'];

//logout script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'logout')
{
    unset($_SESSION['login_user']);
    header('Location:login.php');
}
if(isset($_SESSION['login_user']))
{
    if(isset($_REQUEST['pagename']))
        header('Location:'.$pagename.'.php');
    else
        header('Location:admin.php');
}
else
{
    //display the error msg if the login credentials are wrong!
    if(isset($_SESSION['login_msg']))
    {
        echo 'Wrong username and password !';
        unset($_SESSION['login_msg']);
    }
}
?>

除此之外,在重定向到另一个 URL 之前,您可以使用几行session_write_close();来确保会话数据和/或状态确实已保存/更新。不过,我没有在上面的更正中实现这一点。把它当作一个训练机会。;)

于 2013-08-19T21:27:33.563 回答