dongxie3352 2011-11-15 12:00
浏览 43
已采纳

在用户接受条款/责任消息后,如何根据初始URL输入分配变量php重定向?

see: http://www.eat-drink-etc.com/

I have this code in the header of all the sites' pages:

<?php
session_start();
if (!isset($_SESSION['responsibility'])) {
    //echo '{redirect="responsibility/message"}';
    echo "<script type='text/javascript'>window.location = '{site_url}responsibility/message'</script>";
}?>

Redirecting to mydomain/responsibility/message if the user is a first time (or hasn't visited recently) visitor to the site.

In the message page I have the following:

<?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="/"}';
    }
}?>

The page uses a from input to enter the site:

<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>

Example: if a user goes to http://www.eat-drink-etc.com/articles/fi_europe_ni_2011 they will be redirected to the responsibility/message page. When enter is clicked the user is taken to the home page. (as specified)

How do I redirect to the originally targeted url??? eg. ../articles/fi_europe_ni_2011

  • 写回答

2条回答 默认 最新

  • dq62957 2011-11-15 12:25
    关注

    Fisrtly, why are you performing the browser redirect through Javascript (client side) when you could do the same thing server side with a header("Location: $url;"); call?

    <?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.
    

    Something like that, to start with.

    To answer your question: You would need to retrieve the current URL and store it as a session property before redirecting to the messages page. To amend my above example:

     <?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'];
    

    // etc

    You could then access this URL on the confirmation page by again performing a header redirect like this:

    header("Location: {$_SESSION['last_url']}");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?