2

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>';
?>
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. Commented Jul 21, 2014 at 3:57
  • How do i do that both things on same page Commented Jul 21, 2014 at 3:58
  • I am doning as displayed above but its not creating cookie in browser 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. Commented Jul 21, 2014 at 3:59

3 Answers 3

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.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>
';
Sign up to request clarification or add additional context in comments.

5 Comments

I like this idea better!
I have to run it in javascript and cookie is not the issue I am adding facebook ads pixel code in it which is in javascript.
@HuzoorBux well, if you have a javascript logic to prioritize first, then you can't use header(), either, use window.location.href of javascript in the end of that logic or the html redirect
I did that but i want my users to share thae url in facebook and it shows google.com data and description image etc when we share link on facebook.
you can add customized facebook OG meta tags and set them either statically or dynamically. developers.facebook.com/docs/opengraph/howtos/…
1

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.com");
?>

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!

2 Comments

Warning: Cannot modify header information - headers already sent by (output started at /hermes/redirect.php:92) in /hermes/redirect.php on line 95
Use the example above. Or use javascript for the redirect. My example is bad and untested. Just thought id try to help. Ghost has the better idea though.
0
<meta http-equiv="refresh" content="5; url=exemple.com">

if you need add a message, you can simply set

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.