Page 1 of 1

limiting a session

Posted: Mon Mar 17, 2008 4:03 am
by tabatsoy
Good Day Masters!
how can i limit a session in php. For example i want to limit a session to view my page only once.
thanks in advance.

Re: limiting a session

Posted: Wed Mar 19, 2008 7:46 pm
by www.WeAnswer.IT
You don't need a session, a cookie will work just fine. If the guest has cookies disabled, though, this code will not work.

The only way to make sure people only see your page once is by recording their IP address, and even that doesn't work every single time. To record the IP address, you would need a database.

But, to answer you question, put this at the top of the page you want people to visit only once:

Code: Select all

<?php
if(isset($_COOKIE['viewedAlready']))
{
    // this person has the cookie already
    die("Sorry, you have seen this page already.");
}
else
{
    // this person doesn't have the cookie
    // so we'll give it to them, then show the page
    setcookie("viewedAlready", "yes");
}
?>

Re: limiting a session

Posted: Thu Mar 20, 2008 12:46 am
by Christopher
You could do the same thing with sessions. Just check to see if a session_id() exists yet. If it does then they have been to one page so deny access. You could also put a counter in either the session or a cookie and increment it to limit it to a specific number of page views. Make sure that there is session code on every page.