[英]PHP Redirect after login
Im new at PHP. 我是PHP新手。 Can you help me with redirect after login? 登录后可以帮我重定向吗?
After login redirect goes to /user_menu.php . 登录后重定向到/user_menu.php 。 I want the same: if i login from page /browse.php?id=198 (it is the category link), after login i redirect to the same page, not to user_menu.php. 我也要这样:如果我从/browse.php?id=198页面登录(这是类别链接),登录后我重定向到同一页面,而不是user_menu.php。
Sorry for dublicate question. 对不起,有重复的问题。 Im just want to share with you my code. 我只想与您分享我的代码。 Thanks for helping :) 感谢您的帮助:)
Login form 登录表单
<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>
Full user_login.php code 完整的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';
?>
To redirect to another page right after login, just use a redirect: 要在登录后立即重定向到另一个页面,只需使用重定向:
header('Location: http://www.yoursite.com/whatever-page.php'); exit();
EDIT: 编辑:
Sorry, I got it wrongly. 抱歉,我弄错了。 You want to go back to whatever page you logged in from, right? 您想回到登录页面,对吗?
I would suggest to set a cookie on click. 我建议点击设置Cookie。 If you're using jquery, when user submit the login form you can save a cookie with the current url. 如果您使用的是jquery,则当用户提交登录表单时,您可以使用当前URL保存一个cookie。
Then after login, read that cookie with php and redirect the user (or you could also redirect using jquery). 然后登录后,使用php阅读该cookie并重定向用户(或者您也可以使用jquery进行重定向)。
Have a look at this code for more information: 请查看以下代码以获取更多信息:
https://github.com/carhartl/jquery-cookie
https://github.com/allmarkedup/purl
http://stackoverflow.com/questions/1200266/submit-a-form-using-jquery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.