Jump to content

Sessions do work enough like cookies afterall?


jeffff

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.";

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
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.