Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
登录后PHP重定向_Php_Redirect_Login - Fatal编程技术网

登录后PHP重定向

登录后PHP重定向,php,redirect,login,Php,Redirect,Login,我是PHP新手。登录后你能帮我重定向吗 登录后,重定向到/user_menu.php。我想要相同的:如果我从page/browse.php?id=198(这是类别链接)登录,登录后我重定向到同一页面,而不是user_menu.php 对不起,这个问题很复杂。我只想和你分享我的代码。谢谢你的帮助:) 登录表单 <form class="form" name="login" action="{SSLURL}user_login.php" method="post" id="login-nav

我是PHP新手。登录后你能帮我重定向吗

登录后,重定向到/user_menu.php。我想要相同的:如果我从page/browse.php?id=198(这是类别链接)登录,登录后我重定向到同一页面,而不是user_menu.php

对不起,这个问题很复杂。我只想和你分享我的代码。谢谢你的帮助:)

登录表单

<form class="form"  name="login" action="{SSLURL}user_login.php" method="post" id="login-nav">
    <div class="form-group">
        <label class="sr-only" for="exampleInputEmail2">{L_003}</label>
        <input class="form-control" type="text" name="username" id="username" placeholder="{L_003}">
    </div>
    <div class="form-group">
        <label class="sr-only" for="exampleInputPassword2">{L_004}</label>
        <input type="password" name="password" id="password" class="form-control" placeholder="{L_004}">
        <div class="help-block text-right"><a href="{SITEURL}forgotpasswd.php">{L_215}</a></div>
    </div>
    <div class="form-group">
        <button type="submit" name="action" class="btn btn-primary btn-block">{L_275}</button>
    </div>
    <div class="checkbox">
        <label>
            <input for="rememberme" type="checkbox"> {L_25_0085}
        </label>
    </div>
</form>

{L_003}
{L_004}
{L_275}
{L_25_0085}
完整的user_login.php代码

<?php

include 'common.php';

$NOW = time();

if ($system->SETTINGS['https'] == 'y' && $_SERVER['HTTPS'] != 'on')
{
    $sslurl = str_replace('http://', 'https://', $system->SETTINGS['siteurl']);
    $sslurl = (!empty($system->SETTINGS['https_url'])) ? $system->SETTINGS['https_url'] : $sslurl;
    header('location: ' . $sslurl . 'user_login.php');
    exit;
}

if (isset($_POST['action']) && isset($_POST['username']) && isset($_POST['password']))
{
    $password = md5($MD5_PREFIX . $_POST['password']);
    $query = "SELECT id, hash, suspended FROM " . $DBPrefix . "users WHERE
            nick = '" . $system->cleanvars($_POST['username']) . "'
            AND password = '" . $password . "'";
    $res = mysql_query($query);
    $system->check_mysql($res, $query, __LINE__, __FILE__);
    if (mysql_num_rows($res) > 0)
    {
        // generate a random unguessable token
        $_SESSION['csrftoken'] = md5(uniqid(rand(), true));
        $user_data = mysql_fetch_assoc($res);
        if ($user_data['suspended'] == 9)
        {
            $_SESSION['signup_id'] = $user_data['id'];
            header('location: pay.php?a=3');
            exit;
        }

        if ($user_data['suspended'] == 1)
        {
            $ERR = $ERR_618;
        }
        elseif ($user_data['suspended'] == 8)
        {
            $ERR = $ERR_620;
        }
        elseif ($user_data['suspended'] == 10)
        {
            $ERR = $ERR_621;
        }
        else
        {
            $_SESSION['WEBID_LOGGED_IN']        = $user_data['id'];
            $_SESSION['WEBID_LOGGED_NUMBER']    = strspn($password, $user_data['hash']);
            $_SESSION['WEBID_LOGGED_PASS']      = $password;
            // Update "last login" fields in users table
            $query = "UPDATE " . $DBPrefix . "users SET lastlogin = '" . gmdate("Y-m-d H:i:s") . "' WHERE id = " . $user_data['id'];
            $system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
            // Remember me option
            if (isset($_POST['rememberme']))
            {
                $remember_key = md5(time());
                $query = "INSERT INTO " . $DBPrefix . "rememberme VALUES (" . $user_data['id'] . ", '" . $remember_key . "')";
                $system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
                setcookie('WEBID_RM_ID', $remember_key, time() + (3600 * 24 * 365));
            }
            $query = "SELECT id FROM " . $DBPrefix . "usersips WHERE USER = " . $user_data['id'] . " AND ip = '" . $_SERVER['REMOTE_ADDR'] . "'";
            $res = mysql_query($query);
            $system->check_mysql($res, $query, __LINE__, __FILE__);
            if (mysql_num_rows($res) == 0)
            {
                $query = "INSERT INTO " . $DBPrefix . "usersips VALUES
                        (NULL, '" . $user_data['id'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', 'after','accept')";
                $system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
            }

            // delete your old session
            if (isset($_COOKIE['WEBID_ONLINE']))
            {
                $query = "DELETE from " . $DBPrefix . "online WHERE SESSION = '" . strip_non_an_chars($_COOKIE['WEBID_ONLINE']) . "'";
                $system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
            }

            if (in_array($user_data['suspended'], array(5, 6, 7)))
            {
                header('location: message.php');
                exit;
            }

            if (isset($_SESSION['REDIRECT_AFTER_LOGIN']))
            {
                $URL = str_replace('\r', '', str_replace('\n', '', $_SESSION['REDIRECT_AFTER_LOGIN']));
                unset($_SESSION['REDIRECT_AFTER_LOGIN']);
            }
            else
            {
                $URL = 'user_menu.php';
            }

            header('location: ' . $URL);
            exit;
        }
    }
    else
    {
        $ERR = $ERR_038;
    }
}

$template->assign_vars(array(
        'ERROR' => (isset($ERR)) ? $ERR : '',
        'USER' => (isset($_POST['username'])) ? $_POST['username'] : ''
        ));

include 'header.php';
$template->set_filenames(array(
        'body' => 'user_login.tpl'
        ));
$template->display('body');
include 'footer.php';
?>

要在登录后立即重定向到另一个页面,只需使用重定向:

header('Location: http://www.yoursite.com/whatever-page.php'); exit();
编辑:

对不起,我弄错了。你想回到你登录的任何页面,对吗

我建议在单击时设置cookie。如果您使用的是jquery,当用户提交登录表单时,您可以使用当前url保存cookie

然后在登录之后,用php读取cookie并重定向用户(或者也可以使用jquery重定向)

有关更多信息,请查看此代码:

https://github.com/carhartl/jquery-cookie
https://github.com/allmarkedup/purl
http://stackoverflow.com/questions/1200266/submit-a-form-using-jquery

您可以看到,特别是在“HTTP_REFERER”下阅读。您真的不应该使用PHP来处理密码安全性。如果您使用的PHP版本低于5.5,您可以使用
密码\u hash()
。请。已在PHP7中删除。了解使用PDO的语句,并考虑使用PDO。您可以在会话中添加引用URL,并在登录后重新访问它。抱歉,但对我来说很难做到这一点。我会读,但我不擅长php和jq:(我理解,但这比你想象的要简单,如果你也在做前端开发,你应该学习一些jquery。你也可以只使用php,在你的登录表单中添加一个隐藏字段和当前url,然后在提交后,你可以重定向到它。谢谢,但你能帮我吗?我尝试了这个: