SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problems with PHP Sessions

    Hi Everyone,
    I'm trying to use sessions to validate that a user has visited a certain page and clicked on a link that initiated a script, rather than just typed in the url of the script into a browser.
    To do this i basically set up a session variable and saved it as 1, then in the php script i took that session variable and then incremented it by one, and then checked to make sure the variable is now two, yet no matter what i do the script functions as if php is just skipping over this part as it has had no effect on how the script works or on what it does, in other words everything works as if i changed nothing.

    Here is the first document that the user needs to visit before executing the script (i took out some of the html/css/javascript so that you guys dont have to go through it, i dont think it is affecting it anyway):
    HTML Code:
    <?php
    session_start();
    $_SESSION['valid']=1;
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    //sample link
    <a href="http://anothersite.com/" target="_blank" id="img" onclick="window.open('site/counter-main/site1Handler.php', 'newwin')"><img class="logos" alt="#" title="#" src="site/images/main/image1.jpg" /></a>
        <a class="logo">External Site 1</a>
    Here is the actual script again i shortened it to just the part that deals with the session
    PHP Code:
    session_start();
    $valid $_SESSION['valid'];
    $sessionID $valid 1;
    if(
    $sessionID != 2){
        echo 
    '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=error/error.php">;';
    }else{
    //execute script 
    Thanks alot
    PHP is FUN

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I just tried your scripts and they work as expected.

    On your second script, echo $valid and see what you got. Perhaps it will give a hint as what went wrong.

    Note that I got a 1 for $valid.

  3. #3
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks tom8,
    i did that and i got 1 each time and when i tried sessionID i got 2, so then i tried it by just directly inputting the url of the script and it worked..., however on a different browser it didn't, until i went to the first page and then if i inputted the url it worked, so i think it has something to do with the way the browser records session variables, i think i need to destroy the variable at some point but i don't know where, any ideas?
    PHP is FUN

  4. #4
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i ended up putting it in the script and it rid me of most problems except if someone were to visit the first page and then directly type in the url for the second...
    PHP is FUN

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    , i think i need to destroy the variable at some point but i don't know where, any ideas?
    Yes with your code once you have visited the first page once the Session variable will be set to 1, therefore always allowing you to access your script in the second page. If you dont want this behaviour you need to either 'unset' the variable in your if statement or set it to a number other than 1.

    PHP Code:
    if($sessionID != 2){
        echo 
    '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=error/error.php">;';
    }else{
     
    $_SESSION['valid'] = 0// OR
     
    unset($_SESSION['valid]);
    //execute script 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maxdream01 View Post
    ..... except if someone were to visit the first page and then directly type in the url for the second...
    If you want to cure that problem you would need to use AJAX on your onclick to change the $_Session['valid'] variable, that way only by clicking on the link would the script be available.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am not really sure how to use AJAX, but except for that problem i'm not really have any other difficulties, fo for now i think ill leave it as is, thanks anyway though
    PHP is FUN

  8. #8
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When you start a session, I believe a cookie is created for the browser of that session. When a new browser is used, naturally a new cookie will be used thus what you got when you used a different browser.

    A session is usually timed out after about 24 minutes or you could destroy it or reset it as Mandes noted.

  9. #9
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yah i suspected something of the sort, i ended up just using unset() to destroy the session after the script had executed and reset it when the user entered the first page a second time, which eliminated most of the problems, thank you though
    PHP is FUN


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •