Jump to content

Problem with cookies


pwntastic

Recommended Posts

Hello, im having a bit of a problem...im running a function which declares a session or cookie depending on if the user wants to be remembered...after they have been set, im trying to run another function which based on the session type, does something else...the thing is that when my function to declare the session type is run if it is session it executes the 2nd function just fine because $_SESSION['whatever'] is declared...when the session type is a cookie though my 2nd function indicates that no session type has been specified...if i refresh then it will say a cookie session has been started...while my 2nd function says that there is no session type specified, if i view cookies with my browser it shows them....is this something to do with the time it takes to set a cookie or why is this happening?

 

heres an example:

<?php

class test1{
     function funct_1(){
          //This part will  declare session type setcookie or $_SESSION[] = '';
     }
     function funct2(){
          $this->funct_1();
          if(isset($_SESSION['whatever'])){
               echo 'Session has been set'; //this one works fine if user logs in with session
               die();
          }else if($_COOKIE[''whatever]){
               echo 'Cookie has been set';//this is displayed when user logs in with cookie...but has to refresh the page in order for this message to 
                                                      //print although cookie does exist if you check the browser...
               die();               
          }else{
               echo'No session type has been specified';//this is what is printed out when user logs in with cookie at first
          }
     }
}
?>

Link to comment
Share on other sites

     function funct_1(){
          //This part will  declare session type setcookie or $_SESSION[] = '';
     }

 

You probably left out the most important part.

 

If you are using setcookie to set the cookie, it will NOT be available to PHP until the next page load -- it is NOT available to the current script. Maybe you could "fake" it by setting $_COOKIE after the setcookie in that code -- I don't know, I've never tried; but I don't see why not.

Link to comment
Share on other sites

I'm surprised that with everything else PHP tried to do to "help" developers -- magic quotes, register globals, etc -- they did not have the setcookie() function go ahead and create the $_COOKIE entry.

 

However, the reason it does not work is that setcookie() queues up a header to be sent to the browser when the webserver is ready to send. The browser sends cookies when it requests a page. So, at the time that this script was requested (by the browser), the browser did not have a cookie to send, and PHP did not have anything to put in the $_COOKIE array

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.