Jump to content

Recommended Posts

Greetings,

 

Ive been trying to figure out this problem for a while and I admit im stumped. Even though im really new to php I tried to research this (google and all) but I honestly dont know what to look for and dont know where to find the solution.

 

Here is my code. I am trying to compare the session ID with the cookie that the session generates. The way I have it setup is that a previous page creates the session and is then redirected to another page where this code is located.

 

<?php

session_start();

$bob = session_id();

$john = $_COOKIE["cookiesessionID"];

if ( $bob == $john )

{

echo "yes it is alive";

}

else

{

echo "No its not!";

}

?>

 

When I try to execute this code the first time around the if statement is false because the cookie isnt being read. I have my session time limit set to 5 minutes so I know thats not the problem. I looked on my apache error log file and saw that its spitting out the fallowing error

 

PHP Notice:  Undefined index:  cookiesessionID

 

I have to reload the page for it to work properly. I'm really at a loss here, I dont know what the problem is. Any suggestions?

 

 

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/
Share on other sites

To remove the error do:

if(isset($_COOKIE['cookiesessionID'])){
//cookie is set
}else{
//cookie is not set
}

 

Why it's not working if you changed the name, I can't tell. If you are using Firefox you can view what cookies are set. Do you allow cookies to be set on your browser?

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339087
Share on other sites

Why it's not working if you changed the name, I can't tell. If you are using Firefox you can view what cookies are set. Do you allow cookies to be set on your browser?

 

Yes I do. I've even gone and checked firefox to make sure its getting the cookie, which it is.

 

As for that snipit of code. What im trying to do is use the php server as a poor mans way of doing session timeouts. I have the session life span set to a certain time. Anytime a user interacts with my site I want it so it checks to see if the server still has the sessionID. If it dosnt then they will have to log back in/create an account.

 

Like I said before im by no means a pro at php programming but, from the looks of your code it is relying on the client to check to see if they time out via the client cookie. Someone could go into the cookie and change the expire values and potentially stay on for ever. I don't want that.

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339095
Share on other sites

No, all my code is doing is checking if the value is set before you try to use it. You have to put your code back into if for your session checking.

Why not just set a timestamp in the session of when they logged in, and anytime it's older than however long you want, log them out. No need to rely on any cookie values at all.

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339098
Share on other sites

Ok I figured out the problem

 

So I access the first web page which contains the fallowing code

 

<html><body>

<?php

session_start();

header('Location: http://www.dsfdsafsadfsadfsdf.com/session_test.php');

?>

</body></html>

 

That jumps me to the code I originally posted up top.

 

So I went back and checked my cookies again and it turns out the session isnt being carried over from page hearder to page check my sessionID. The session check page is generating a completely new session.  When I look at my cookie I have 2 completely different sessionID's.

 

So question, I though sessions carry over as long as the browser doesn't close?

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339106
Share on other sites

IIRC, Sessions are only on the sub-domain. So http://www.site.com and http://site.com will have different sessions.

Also, session_start() needs to be BEFORE any HTML or any output or headers.

 

Thats exactly whats going on. One variable has the http://www.site.com and the other just has http://site.com.

 

 

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339112
Share on other sites

So there is your problem.

Technically, www is not needed. I always make my sites use only http://site.com. You can use htaccess to do that. I also use a BASE_URL constant so I don't have to keep typing it in.

 

LOL what.....  ???. I don't have the slightest idea how to implement any of that. Well I guess thats why google was invented huh?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/67530-solved-cookie-problem/#findComment-339115
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.