PHP Classes

Problems with Saving and Restoring Cookies

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  Problems with Saving and Restoring...  >  (Un) Subscribe thread alerts  
Subject:Problems with Saving and Restoring...
Summary:Trying to get cookies to other functions within a class.
Messages:15
Author:Chris
Date:2007-07-20 14:32:41
Update:2007-07-23 17:52:47
 
  1 - 10   11 - 15  

  1. Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-20 14:32:41
I have built a class of functions that will control an admin panel. The first function will login, which works perfectly. I have in there right above the close statement this:

$http->SaveCookies($_SESSION['site_cookies']);


then in another function within the same class to do another function on the admin panel I have this:

$http->RestoreCookies($_SESSION['site_cookies'], 1);

which is right above the GetResultsArguments command.

When I execute this I get the following error returned to me: Error: invalid cookie expiry value type (s:19:"2006-07-25 05:00:00";)

I am not sure if I totally understand how to save and restore cookies I guess. I need to be able to login with one function, save the cookies and then allow the retrieval of the cookies for numerous other functions.

Any help would be appreciated.

Thanks,

-- Chris

  2. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-20 14:35:31 - In reply to message 1 from Chris
A quick note: the reason I am trying to save to a session variable is because I was getting a PHP error when using just a regular variable. I think due to the fact that it was a variable only for that specific function, and I need it to be passed to multiple functions within the class.

  3. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-20 14:42:07 - In reply to message 2 from Chris
Another Note:

instead of using a session variable I tried to use a Global variable and the error I get with that is:


Error: invalid cookie secure value type (b:0;)


so, I am still at a loss as to how to allow other functions within the same class to have access to send the cookies back to the admin panel to keep me authinticated.

--- Chris

  4. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-07-20 20:47:37 - In reply to message 1 from Chris
There was a bug in the validation of cookie expiry date. I just uploaded a fixed version. Thank you for reporting the problem. Just let me know if you still have other problems.

  5. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-20 23:45:01 - In reply to message 4 from Manuel Lemos
Manuel,

Thanks for the quick reply. I will get on later tonight and give the new version a try and letcha know if it works for me. I really appreciate your script man, exactly what I was looking for.

--- Chris

  6. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-22 07:19:29 - In reply to message 4 from Manuel Lemos
Manuel,

The whole cookie thing is still not working for me, after some testing on my part here is what I have so far.

First I have a global variable set outside the class called $site_cookies = array();

Second in the first function of the class I pull in the variable with GLOBAL $site_cookies. then right under where I issue the $http->Close(); I issue $http->SaveCookies($site_cookies); ( getting this from your example file)

Third, when I go to execute the second class function to do some other functions within my newly validated session it kicks me back to the login page. on my second function I am calling: $error=$http->RestoreCookies($site_cookies, 1); right underneath $error=$http->Open($arguments);
I am also using your cookie compare script: if(strlen($results=$APIx_processor->RestoreCookies($site_cookies, 1))==0)
{
$APIx_processor->SaveCookies($saved_cookies);
if(strcmp(serialize($saved_cookies), serialize($site_cookies)))
{
$myReturn .= "<H2>FAILED: the saved cookies do not match the restored cookies.</H2>\n";
}
else
$myReturn .= "<H2>OK: the saved cookies match the restored cookies.</H2>\n";
}

and that returns that the cookies match the restored cookies, yet for some reason those cookies are not being passed to the next form call. I don't think I understand your example for how to save and return cookies. My goal is to have a class with a login function to start the session with the admin panel and then have multiple other functions to issue other commands within that panel without having to submit the login string before each admin command. That way I only have to log in once. I just can't seem to get it working. If you could either provide me with a better example of how to save and return cookies based on the way I was wanting to do things, or let me know what additional data I can post to help you explain things better to me I would greatly appreciate it.

Thanks again for your time and help.

a quick side note: before to get it working, I do have a login function, but I call that function within each admin panel function first, and then the function continues with the admin command and it works just fine. The problem with that is, each time I wish to issue an admin command it has to login to the admin panel each time. So say I want to do 4 or so commands. Each time each command is issued It logs me in, I was hoping the session could remain open like it would if I was using it directly.

--- Chris

  7. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-22 07:21:20 - In reply to message 6 from Chris
one thing to note above, anything refering to $results will be $error in your script and $APIx_processor will be $http. I ment to replace the names to match how your have coded your examples, but must have missed those.

  8. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-22 13:49:32 - In reply to message 6 from Chris
I tried something else real quick today, instead of declaring the global variable $site_cookies as an array I just declared it as an empty string $site_cookies = ''; and then tried to run the two functions again, seperatly. The login function runs just fine, but when I try and run an admin panel function I get this error from your class now: Error: invalid cookie secure value type (b:0;) I am not sure if I need to be declaring the global variable as an array in the first place and that is why I am getting this error now, but I just figured I would try as many different things as possible to give you the best picture of what is causing the error, either script side or behind the keyboard side ;)

Thanks again for your help,

--- Chris

  9. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Chris Chris - 2007-07-22 14:05:13 - In reply to message 1 from Chris
Another thing I tried was to change the way I define the array for the global variable incase it did need to be defined as an array. Instead of $site_cookies = array(); I use $site_cookies[] = ''; and when I run the script I get this error instead: Error: invalid cookie domain value type (b:0;)

  10. Re: Problems with Saving and Restoring...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-07-22 21:19:32 - In reply to message 6 from Chris
Restoring cookies may not work if the sessions on the remote server associated to those cookie values have expired. It is like with a normal browser.

I don't know if that is your actual problem, but it can be an explanation if you take too long since you have saved the cookies and restored them to access the same site again.

You need to check the following requests and see if the class is sending to the server the cookies that it received before.

 
  1 - 10   11 - 15