0

我有以下代码将某人登录到我的页面 - 我想弄清楚的是如何调整它,以便一旦它验证他们是一个好的登录名,它将他们转发到不同的页面。

session_name('wpLogin');
// Starting the session

session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks

session_start();

if($_SESSION['id'] && !isset($_COOKIE['wpRemember']) && !$_SESSION['rememberMe'])
{
    // If you are logged in, but you don't have the wpRemember cookie (browser restart)
    // and you have not checked the rememberMe checkbox:

    $_SESSION = array();
    session_destroy();

    // Destroy the session
}


if(isset($_GET['logoff']))
{
    $_SESSION = array();
    session_destroy();

    header("Location: index.php");
    exit;
}

if($_POST['submit']=='Login')
{
    // Checking whether the Login form has been submitted

    $err = array();
    // Will hold our errors


    if(!$_POST['username'] || !$_POST['password'])
        $err[] = 'All the fields must be filled in!';

    if(!count($err))
    {
        $_POST['username'] = mysql_real_escape_string($_POST['username']);
        $_POST['password'] = mysql_real_escape_string($_POST['password']);
        $_POST['rememberMe'] = (int)$_POST['rememberMe'];

        // Escaping all input data

        $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM tz_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));

        if($row['usr'])
        {
            // If everything is OK login

            $_SESSION['usr']=$row['usr'];
            $_SESSION['id'] = $row['id'];
            $_SESSION['rememberMe'] = $_POST['rememberMe'];

            // Store some data in the session

            setcookie('wpRemember',$_POST['rememberMe']);
        }
        else $err[]='Wrong username and/or password!';
    }

    if($err)
    $_SESSION['msg']['login-err'] = implode('<br />',$err);
    // Save the error messages in the session

    header("Location: index.php");
    exit;
4

1 回答 1

1

您需要使用该header功能。文档可以在这里找到:http: //php.net/manual/de/function.header.php

简而言之,使用这个:

if($row['usr']) {
    // If everything is OK login

    $_SESSION['usr']=$row['usr'];
    $_SESSION['id'] = $row['id'];
    $_SESSION['rememberMe'] = $_POST['rememberMe'];

    // Store some data in the session

    setcookie('wpRemember',$_POST['rememberMe']);
    header("Location: index.php");
}

请记住,header只有在向浏览器输出单点符号之前调用任何函数才能工作。

这包括echoPHP 标记之外的任何内容。另外,请注意,您的源文件的格式中没有 BOM,因为这也会导致header失败。

于 2013-03-02T12:29:52.920 回答