jeffff Posted June 18, 2011 Share Posted June 18, 2011 Hello a question about concept from some of you that would really have experience to know the answer to this. For instance: session_start(); if(isset($_SESSION['sessionidname'])){ $z=$_SESSION['sessionidname'];} ELSE {$z=1;} Names and starts a session at a start, for something to be done with $z. But then once browser with a page closes and someone comes back later things start at 1 again. Did not think the same thing can be done with sessions that can be done with cookies. So my questions. I've heard a lot of people saying that sessions can be used like cookies, where after a vistor visits a same page. When they come back to the same page again they can see something new shown, by what they saw last. Is this true? Because I thought when things are showing by sessions when a page is open, once the web page and browser are closed. The sessions ends and when they come back to the same page things start over again from the start. That it is why people use cookies, because that same thing that is done with cookies cannot be done with sessions once the browser page closes. Please let me know if that can be done with sessions like it is cookies as well will really appreciate knowing from you who know that. If so some explanation on how that is done and why it does work like that like cookies do. Since it won't help me any if I don't even know why that would work with sessions as well. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/ Share on other sites More sharing options...
gizmola Posted June 18, 2011 Share Posted June 18, 2011 Yes sessions can do what cookies do, and even more importantly, the data stays on the server. Try: session_start(); if (!isset($_SESSION['pagecount'])) { $_SESSION['pagecount'] = 1; } else { $_SESSION['pagecount']++; } echo 'Session tester'; echo "Ran this {$_SESSION['pagecount']} times."; Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231554 Share on other sites More sharing options...
jeffff Posted June 18, 2011 Author Share Posted June 18, 2011 Thank you very much. Why do people even use cookies so often then? So how does your example of what was shown last time for someone. How does that remember each different visitor so that it knows what it already showed them the last time. I'm looking hard but I'm not spotting it. Can someone break that down for me why the code just listed can know to show someone a next thing than what they showed some visitor last? Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231564 Share on other sites More sharing options...
gizmola Posted June 18, 2011 Share Posted June 18, 2011 By default php sessions actually work with a cookie. It's a session cookie that simply has an id in it. On subsequent page views, php matches up any $_SESSION variables it has stored with that session id. The default is that these session variables are stored in small files on the server. Therein lies the advantage, as unlike cookies, the data you store in the session variables is never sent to the client. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231571 Share on other sites More sharing options...
jeffff Posted June 18, 2011 Author Share Posted June 18, 2011 I can tell you are very knowledgeable on this matter. Let me know if anyone who knows disagrees with that. Thank you very much. So when someone clears all their cookies on their browser does it clear the session id and cookies also? If it does clear session id's also when someone clears all their cookies, why would anyone choose sessions or cookies over the other one? Please let me know thank you. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231574 Share on other sites More sharing options...
xyph Posted June 18, 2011 Share Posted June 18, 2011 That's how it works. Sessions by default, are short-term. They are usually destroyed in a short time or when the browser is closed. Cookies can be set to expire in a year. Sessions can be set to be long-term, but this isn't suggested, as it requires cookies and some have cookies disabled. When cookies are disabled, PHP will pass session data in the request string. This makes persisting the session beyond a browser close very awkward. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231581 Share on other sites More sharing options...
jeffff Posted June 18, 2011 Author Share Posted June 18, 2011 Sounds like you both are differing on one or more of these points, do you agree with ALL they just said gizmola? Ok so let me know if this isn't what any of you are saying: Sessions can go much longer than a browser close but it is extremely hard to do? Also if session are set to go longer than a browser close, if anyone clears their cookies it will clear all the session id's also. Let me know if that is incorrect anyone who knows. Thank you very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231589 Share on other sites More sharing options...
xyph Posted June 18, 2011 Share Posted June 18, 2011 No, we agree. gizmola doesn't mention anything about persistent sessions, because that's not what they're meant for. You're correct. Sessions _CAN_ go longer, but you shouldn't use them like that. If a user wipe's their cookies, the link to that session will be destroyed. An exception is if the session is being tracked through the request URI ( the pages have ?PHPSESSID=... in their URI ), in which case clearing cookies won't destroy the link. Cookies are NOT sessions. Cookies are stored client-side. Sessions are stored on the server. A session can use a cookie to link a client with specific session data. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231597 Share on other sites More sharing options...
jeffff Posted June 18, 2011 Author Share Posted June 18, 2011 Getting last, ok you seem to know a lot about it also. It sounds like that's why there isn't a point to using sessions some of the time, except for certain situations, because cookies can be set a lot longer pretty easily. I agree with what you said cookies aren't sessions. But it sounds like if cookies are cleared that also destroys session id's also?? That go in here: $_SESSION['asessionid'] doesn't clearing cookies destroy that just said session id as well? That's an important questiion that many are never very clear about would really help to know for future on what there is need to use. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231604 Share on other sites More sharing options...
xyph Posted June 19, 2011 Share Posted June 19, 2011 No. The session id will exist until it expires, or a PHP script destroys it. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231626 Share on other sites More sharing options...
jeffff Posted June 19, 2011 Author Share Posted June 19, 2011 Amazing someone after a few years finally answered that question. Thank you very much for your great help!!!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231629 Share on other sites More sharing options...
gizmola Posted June 19, 2011 Share Posted June 19, 2011 Try reading my reply at the end of this thread, and see if that clears anything up: http://www.phpfreaks.com/forums/index.php?topic=223785.msg1579550#msg1579550 Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231636 Share on other sites More sharing options...
jeffff Posted June 19, 2011 Author Share Posted June 19, 2011 That you did was fantastic thank you very much for telling me about that since you could tell it would help me, and I was wondering about it. Thanks a lot. I am more clear about those things now. Quote Link to comment https://forums.phpfreaks.com/topic/239745-sessions-do-work-enough-like-cookies-afterall/#findComment-1231832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.