PHP 如何在成功登录时将用户重定向到索引.html


php how to redirect user to index.html when sucessfully logging in

嗨,我的网站的登录脚本有问题,我需要脚本来重新整理用户以编制索引.html如果登录详细信息正确。 如果你能帮助我,将不胜感激。 谢谢...

这是我检查详细信息的脚本==>

<?php
include('config.php');
?>
$ousername = '';
    //We check if the form has been sent
    if(isset($_POST['username'], $_POST['password']))
    {
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
            $ousername = stripslashes($_POST['username']);
            $username = mysql_real_escape_string(stripslashes($_POST['username']));
            $password = stripslashes($_POST['password']);
        }
        else
        {
            $username = mysql_real_escape_string($_POST['username']);
            $password = $_POST['password'];
        }
        //We get the password of the user
        $req = mysql_query('select password,id from users where username="'.$username.'"');
        $dn = mysql_fetch_array($req);
        //We compare the submited password and the real one, and we check if the user exists
        if($dn['password']==$password and mysql_num_rows($req)>0)
        {
            //If the password is good, we dont show the form
            $form = false;
            //We save the user name in the session username and the user Id in the session userid
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['userid'] = $dn['id'];
?>
<?php
        }
        else
        {
            //Otherwise, we say the password is incorrect.
            $form = true;
            $message = 'The username or password is incorrect.';
        }
    }
    else
    {
        $form = true;
    }
    if($form)
    {
        //We display a message if necessary
    if(isset($message))
    {
        echo '<div class="message">'.$message.'</div>';
    }
    //We display the form
?>

任何帮助将不胜感激..谢谢。

更新:正如@Dagon纠正我的那样。

若要将用户重定向回 index.html,可以使用以下命令:

header('Location: http://example.com/index.html');
exit;

成功登录后。

因此,您的源代码不是这种登录方式的最佳选择,但即使以这种方式,您也应该在登录后进行重定向。

你可以用JavaScript来做,我建议你使用jQuery API,这将帮助您改进网站上的许多内容。

因此,在您的情况下,我建议您使用这种解决方案:

 if($dn['password']==$password and mysql_num_rows($req)>0)
        {
            //If the password is good, we dont show the form
            $form = false;
            echo "<script>$('#idofelement2reload').load('php.php?login=1');</script>";
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['userid'] = $dn['id'];
        }

所以如果你注意到,这行

echo "<script>$('#idofelement2reload').load('php.php?login=1');</script>";

将一行写入HTML文档,该文档调用一个函数通过jQuery加载带有用户登录的参数的文件。

不要忘记在head中包含jQuery源代码

$_SESSION 中设置用户信息后使用 header() 函数。

// If the password is good, we don't show the form
$form = false;
// We save the user name in the session username and the user Id in the session userid
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
header('Location: http://example.com/index.html');
if($dn['password']==$password and mysql_num_rows($req)>0)
        {
            //If the password is good, we dont show the form
            $form = false;
//We save the user name in the session username and the user Id in the session userid
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['userid'] = $dn['id'];
       header('Location:http://sitename.com/index.php');
?>