php redirect - session cookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
solmartin
Forum Newbie
Posts: 3
Joined: Mon Aug 17, 2009 12:18 pm

php redirect - session cookie

Post 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!
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: php redirect - session cookie

Post 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.
solmartin
Forum Newbie
Posts: 3
Joined: Mon Aug 17, 2009 12:18 pm

Re: php redirect - session cookie

Post by solmartin »

Thanks! That worked quite easily.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: php redirect - session cookie

Post by AlanG »

Cool. Your welcome. :)
solmartin
Forum Newbie
Posts: 3
Joined: Mon Aug 17, 2009 12:18 pm

Re: php redirect - session cookie

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: php redirect - session cookie

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