Page 1 of 1

php redirect - session cookie

Posted: Mon Aug 17, 2009 12:33 pm
by solmartin
Hi there,

I am quite new, and I hope someone can assist me, as I am a little confused.

I am attempting to write a quick little script that does the following:
When index.php is opened, it automatically redirects to index.html (until the user selects Home). Also, I want it to store a single-session cookie (ie: until the browser window closes) so that anytime a visitor selects the home button during the visit, it goes to index.php.

I have searched the net and the best pieces of code I have found are

Code: Select all

 
setcookie('SplashCookie','1',time()+(60*60*24*365));
 
and
 
ob_start();
echo "Test";
header("Location: http://www.myurl.com/index.html");
ob_flush();
But I am not sure how to combine the two and make it apply to just the browser session (and not a full day). Any ideas?

If anyone can help, it would be appreciated.
Thanks!

Re: php redirect - session cookie

Posted: Mon Aug 17, 2009 12:52 pm
by AlanG
If you are only concerned about storing information about the visitor until the browser is closed, then there is no need to use a cookie. Cookie's are used to prolong the storage of information beyond the browser's lifetime. :)

I'm not entirely sure what you mean so just to recap.
  • Vistor goes to yourdomain.com which opens up index.php
  • You then want to check if they already have a session open, if they do, leave them on that page but change the link to home to say index.php, otherwise redirect them to index.html
Ok here goes:

Code: Select all

 
<?php
    if(!isset($_SESSION['active']) || empty($_SESSION['active'])) {
        $_SESSION['active'] = true;
        header("Location: http://".$_SERVER['SERVER_NAME']."index.html");
    }
    else
        $_SESSION['active'] = false;
?>
<html>
    <!-- You can put your html code here -->
    <a href="<?php ($_SESSION['active']) ? echo 'index.php' : echo 'index.html'; ?>">Home</a>
</html>
 
You don't need to use output buffering in this case. One advantage of using output buffering is that you can clear any previous output. This has it's benefits when you want to add headers to the page (such as a redirect or a cookie). The $_SESSION array will only store values while the browser remains open and will be cleared when it is closed.

Re: php redirect - session cookie

Posted: Mon Aug 17, 2009 1:33 pm
by solmartin
Thanks! That worked quite easily.

Re: php redirect - session cookie

Posted: Mon Aug 17, 2009 10:59 pm
by AlanG
Cool. Your welcome. :)

Re: php redirect - session cookie

Posted: Tue Aug 18, 2009 8:49 am
by solmartin
just out of curiosity, in case I need to add this on a page that is .html: what is the correct way to add a .htaccess handler? I've seen both of these numerous times online, and it looks strikingly contradictory:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

and

AddHandler cgi-script .html .htm

Thanks!

Re: php redirect - session cookie

Posted: Tue Aug 18, 2009 9:00 am
by jackpf
I'd go for the first. I think the second is for if you have PHP installed as CGI binary or something, although I'm not sure...