admin管理员组

文章数量:1430526

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.

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

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

Code:

<?php
header("location: ");
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>';
?>

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

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

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

Code:

<?php
header("location: https://www.google.");
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>';
?>
Share Improve this question edited Oct 29, 2014 at 5:03 Kevin 41.9k12 gold badges56 silver badges72 bronze badges asked Jul 21, 2014 at 3:54 Huzoor BuxHuzoor Bux 1,0484 gold badges23 silver badges46 bronze badges 4
  • It appears you are taking the user immediately to the location. You will want to be doing your functions for cookies before sending the header information. The header must happen before the html tag though. So run the java before the html as well. – jjonesdesign Commented Jul 21, 2014 at 3:57
  • How do i do that both things on same page – Huzoor Bux Commented Jul 21, 2014 at 3:58
  • I am doning as displayed above but its not creating cookie in browser – Huzoor Bux Commented Jul 21, 2014 at 3:59
  • 1 Correct, because the header is sending the user to the new page before your code is executed. working on a sample one moment. – jjonesdesign Commented Jul 21, 2014 at 3:59
Add a ment  | 

3 Answers 3

Reset to default 3

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./'); 
}

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.">';
}

On Javascript:

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

Give this a shot. I haven't test it, but it will give you the right idea to start with:

<?php ?>
       <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>
<?php
header("location: https://www.google.");
?>

I opened and closed the php tag in the beginning because I am assuming this will be in a php file. You may also put your cookie code in a separate .html or .php file and use a include before your header.

include_once 'cookiecode.php';

The trick with headers is getting your task done before presenting any html that displays to the screen. I am pretty certain headers also have to be done before the open tag.

Hope this helps!

<meta http-equiv="refresh" content="5; url=exemple.">

if you need add a message, you can simply set

<h1>Redirection en cours... Veuillez patienter.</h1>

本文标签: phpRedirect with header and javascript codeStack Overflow