doudeng1870 2014-07-21 03:54
浏览 44

使用标头和JavaScript代码重定向

I created a redirect page in PHP that whenever some one visits that page, it will add a cookie in browser and redirect user to some other page: ex: google.com.

To do that, I have used javascript to redirect but problem is that when I extract my url it doesn't show google.com. During extraction it shows the same page's information then I use php header() for redirection and it shows me the google.com info.

Now I need help to make this code work with cookie and header.

Code:

<?php
header("location: https://www.google.com");
echo '<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>Redirecting...</title>
        <script type="text/javascript">
        function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }
        createCookie("karachi","testing",100);
        </script>
    </head>
    <body>

    </body>
</html>';
?>
  • 写回答

2条回答 默认 最新

  • dongxi1943 2014-07-21 04:03
    关注

    Instead of setting it on JavaScript. Why not use PHP.

    $date_of_expiry = time() + (86400 * 30); // 30 days
    if(setcookie('karachi', 'testing', $date_of_expiry)) {
        sleep(3); // sleep 3 seconds
        header('Location: http://www.google.com/'); 
    }
    

    Or an alternative:

    $date_of_expiry = time() + (86400 * 30); // 30 days
    if(setcookie('karachi', 'testing', $date_of_expiry)) {
        echo '<h1>Redirecting.. Please wait.</h1>';
        echo '<meta http-equiv="refresh" content="3;url=http://www.google.com">';
    }
    

    On Javascript:

    echo '<h1>Redirecting.. Please wait.</h1>';
    // echo 'logic javascript';
    echo '
    <script>
    setTimeout(function(){
        window.location.href = "http://www.google.com"
    }, 3000);
    </script>
    ';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程