请参阅:http://www.eat-drink-etc.com/
我在所有站点页面的页眉中都有以下代码:
<?php
session_start();
if (!isset($_SESSION['responsibility'])) {
//echo '{redirect="responsibility/message"}';
echo "<script type='text/javascript'>window.location = '{site_url}responsibility/message'</script>";
}?>如果用户是第一次(或最近没有访问过)该站点的访问者,则重定向到mydomain/responsibility/message。
在消息页面中,我有以下内容:
<?php
session_start();
/*if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
$_SESSION = array();*/?>function setcookielive($name, $value='', $expire=0, $path='', $domain='', $secure=false, $httponly=false) {
//set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
$_COOKIE[$name] = $value;
return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
}
if(isset($_POST['set'])) {
if(isset($_POST['remember'])) {
/*if(setcookielive("responsibility", "confirmed", time()+60*60*24*30*24, "/")) {
}*/
$_SESSION['responsibility'] = 'confirmed';
echo '{redirect="/"}';
}
else {
/*if(setcookielive("responsibility", "confirmed", time()+60*60*24, "/")) {
}*/
$_SESSION['responsibility'] = 'confirmed';
echo '{redirect="/"}';
}
}?>该页面使用from输入进入站点:
<form method="post" action="">
<input type="hidden" name="set" value="set" />
<input type="hidden" name="remember" value="true" />
<input type="image" src="{site_url}images/elements/enter-btn.png" width="95" height="26" alt="Enter" value="Enter" />
</form>示例:如果用户转到http://www.eat-drink-etc.com/articles/fi_europe_ni_2011,他们将被重定向到责任/消息页面。单击enter时,用户将转到主页。(指定)
如何重定向到最初的目标url?例如:../文章/fi_europe_ni_2011
发布于 2011-11-15 20:25:58
顺便说一句,你为什么要通过Javascript (客户端)执行浏览器重定向,而你可以通过header("Location: $url;");调用在服务器端做同样的事情?
<?php
ob_start();
session_start();
if (!isset($_SESSION['responsibility'])) {
ob_end_clean();
header("Location: $url; ");
exit;
}
// etc... you would need to call ob_end_flush(); before ending the page if you plan to have output here.类似这样的东西,首先。
回答您的问题:在重定向到消息页面之前,您需要检索当前URL并将其存储为会话属性。为了修正我上面的例子:
<?php
ob_start();
session_start();
$_SESSION['last_url'] = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];//等
然后,您可以通过再次执行标题重定向在确认页面上访问此URL,如下所示:
header("Location: {$_SESSION['last_url']}");发布于 2011-11-15 20:24:21
你可以这样做:
<?php
session_start();
if (!isset($_SESSION['responsibility'])) {
header("Location: /responsibility/message?refer=" . urlencode($_SERVER["REQUEST_URI"]));
}?>在你的职责页面上,你可以这样做:
if(isset($_POST['set'])) {
if(isset($_POST['remember'])) {
$_SESSION['responsibility'] = 'confirmed';
if( isset($_GET['refer']) )
header("Location: " . $_GET['refer']);
}
}您可能需要向$_ get‘’refer‘添加一些检查,这样它就不会被滥用,但是您已经明白了
https://stackoverflow.com/questions/8136014
复制相似问题