473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Understanding Sessions/Cookies

23
Dear All,

Ok. Here I am again for some answers. I've gotten to understand more of things from here than other places. I hope I don't get spoiled. LOLZ...

Associative Array is an array that uses names or strings as indeces of the data stored (instead of the usual 0,1,2.. numbers). Am I right? So, in $_SESSION, I can store $_SESSION['views'], $_SESSION['timeLog'], or whatever I want as many as I can (discretely) in one user session, right? And, I can call it all in the different pages of the site in one server one session, right? And it all get's destroyed upon leaving the site, right?

It goes the same for $_COOKIES except that... that's what I don't know... the exceptions. The two has so many in common now I don't know what the true difference is. I mean, I know that cookies are set on the client's PC, while session is for server's use during the session. But, is there more to it than just those two? Can I also create different cookie values during the session and destroy those without destroying the real cookie stored in the client's PC while it hasn't expired? Using both at the same time is the way to go, is it really?

These and many more are things that confuses me with both cookies and sessions. Any input will really help in drawing the line between the two. Please feel free to criticize, holler, or blast any info for me. I'd appreciate it. Don't get me wrong, I like cookies better than session. They're delicious. Session, it depends whether it's a pot session, friendly make out session, or whatever... LOLZ... Oh, and I've read those tutorials at Tizag, w3s, php.net, etc... I just need to here it from other folks how they understand both to better understand it myself.

When else and where else to use which?

My apologies. Aparently, for beginners like me, knowing both seem more confusing.

@Markus (and the other moderators)

Please share your wisdom for me and other beginners out there.

Thanks

rsbgm
Apr 4 '09 #1
11 1777
rsbgm
23
ps: Do I have to set sessions and/or cookies on each page of the site?


rsbgm
Apr 4 '09 #2
Markus
6,050 Expert 4TB
Dear All,

Ok. Here I am again for some answers. I've gotten to understand more of things from here than other places. I hope I don't get spoiled. LOLZ...
It's ok, as long as you don't get selfish! Give a little back to the community when you can.

Associative Array is an array that uses names or strings as indeces of the data stored (instead of the usual 0,1,2.. numbers). Am I right? So, in $_SESSION, I can store $_SESSION['views'], $_SESSION['timeLog'], or whatever I want as many as I can (discretely) in one user session, right? And, I can call it all in the different pages of the site in one server one session, right? And it all get's destroyed upon leaving the site, right?
You can name associative arrays with alphanumeric characters, and a few others (underscores).

Yes, they are destroyed when the browser is closed. They can be destroyed earlier, manually (session_destroy(), unset()) or systematically (depending on how you set the sessions lifetime).

It goes the same for $_COOKIES except that... that's what I don't know... the exceptions. The two has so many in common now I don't know what the true difference is. I mean, I know that cookies are set on the client's PC, while session is for server's use during the session. But, is there more to it than just those two?
Not a whole lot of difference, tbh. You know the main difference. What makes them different is how you should be using them: Cookies should not hold sensitive data, not to mention un-encrypted data, as they are visible to the end-user.

Can I also create different cookie values during the session and destroy those without destroying the real cookie stored in the client's PC while it hasn't expired? Using both at the same time is the way to go, is it really?
I don't understand this part.

These and many more are things that confuses me with both cookies and sessions. Any input will really help in drawing the line between the two. Please feel free to criticize, holler, or blast any info for me. I'd appreciate it. Don't get me wrong, I like cookies better than session. They're delicious. Session, it depends whether it's a pot session, friendly make out session, or whatever... LOLZ... Oh, and I've read those tutorials at Tizag, w3s, php.net, etc... I just need to here it from other folks how they understand both to better understand it myself.

When else and where else to use which?
Again, the main distinction between the use of sessions vs. cookies is the kind of data it will hold. Never allow the end-user to see sensitive data! EVER! ;)

My apologies. Aparently, for beginners like me, knowing both seem more confusing.

@Markus (and the other moderators)

Please share your wisdom for me and other beginners out there.

Thanks

rsbgm
Hope this was a little helpful and cleared some doubts, maybe.

- mark.
Apr 4 '09 #3
Markus
6,050 Expert 4TB
@rsbgm
Only on parts of your site that require them..

session_start() must be called on any page that needs access to the $_SESSION array (unless your php settings start it automatically).

- Mark.
Apr 4 '09 #4
rsbgm
23
@Markus

About the part you didn't understand on cookies...

Let's say at the start of the visit (first time) we set the cookie:

setcookie (name, value, exp);

I understand that u can set another cookie during the session (for some reason you need to in your coding)... and another... and another...:

setcookie(name2, value2, exp2);
setcookie(name3, value3, exp3);
setcookie(name4, value4, exp4);

Now, unlike SESSION which expires after the visit, you can access any of the above cookies on the next visit as long as it hasn't expired. Also, if u want to destroy one cookie, you can by specifying the cookie name.

Is this about right? Can u use cookies this way?

rsbgm
Apr 4 '09 #5
Markus
6,050 Expert 4TB
@rsbgm
Yeah, that's correct. You can set multiple sessions from different regions of your code, too, though. And you can also delete individual sessions, like cookies.

- mark.
Apr 4 '09 #6
rsbgm
23
Wow! Thanks a lot, man!

KUDOS!!!

rsbgm
Apr 4 '09 #7
Markus
6,050 Expert 4TB
@rsbgm
Np, rsbgm.

Keep it real*,

- Mark.

*I'm feelin' cool.
Apr 4 '09 #8
rsbgm
23
You mentioned that in order for SESSION variables to be accessed, you have to start_session() on that page. How about COOKIES? Does it need anything on the top of the page in order to access it? It's practically just gonna check the clients PC for the information everytime you invoke the code, right?

Now... this may be a bit off topic, but, bear with me on this question... (before I make some conclusions)...

POST/GET/REQUEST are associative arrays as well. This is usually set through input forms. This cannot be set like an ordinary variable that you can just assign a value. I understand how it is passed on from the FORM page to the PHP page with the POST code, but, can you also call that POST variable on other pages? Does it also need some code on top of the page that will be accessing the POST variable?

rsbgm
Apr 4 '09 #9
Markus
6,050 Expert 4TB
@rsbgm
POST, GET and COOKIE are all of the same cloth, actually. They're called 'super globals', meaning they're available throughout any php scripts you make - you need not 'instantiate' them or use the 'global' keyword.. REQUEST checks all 3 of the above arrays for whatever key you look for. In fact, SESSION is also a superglobal but does need to be 'started' for access to it to be available - COOKIE does not.

Does that answer your question?

Edit - it doesn't check the clients browser when ever your code is run - only when you access the COOKIE array.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Some stuff here. PHP couldn't care less about COOKI right now.
  3.  
  4. echo $_COOKIE['hello']; // PHP now access the array.
  5. ?>
  6.  
Apr 4 '09 #10
rsbgm
23
Yes, it does. Again, thank you very mucho!

rsbgm
Apr 4 '09 #11
Markus
6,050 Expert 4TB
Keine problem,

- mark.
Apr 4 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kevin Thorpe | last post by:
I've hit a problem with sessions. If I have a document relying on sessions which has a link target=_blank then the new window inherits the session from its parent (in IE). However, if I embed...
1
by: windandwaves | last post by:
Hi Gurus I am basically sorry that I have to bother you about this. I am a PHP beginner and I have been studying sessions and cookies over the last few weeks. I have learned lots, but I am...
6
by: JJ | last post by:
Hi, I really need to use cookieless ASP sessions with ASP 3 (IIS5) Can I find out the session ID from the first page, then post it or send it with the url to the next page, then at the start...
2
by: Steve Franks | last post by:
According to the docs you tell ASP.NET to use cookieless sessions by setting a value in the config.web file. However, what if I wanted to determine at run time whether or not I wanted to use...
2
by: Chris Mahoney | last post by:
Hi I'm using several Sessions in my app. When the user has cookies enabled in their browser, everything works fine. But with cookies disabled, only IE seems to remember the sessions. In Firefox...
7
by: Atte André Jensen | last post by:
Hi I'm developing a site where I'd like to store information during a users visit. So far I've been using sessions, but as far as I can tell it's not possible to control for how long a session...
6
by: Paul | last post by:
Here is a question that should get everyone going. I have an ecommerce site where I need to pass the order_id to every page. So which method is the best practice to pass this variable between...
5
by: jheines | last post by:
I am trying to explain how cookies and sessions work in a class I teach, but I have hit a wall when it comes to the interaction between cookies and the state of the privacy settings in Internet...
8
by: Chuck Anderson | last post by:
I've instituted a sessions based scheme on my web site to combat hot linking to my images. When someone requests a page at my site, I set a session variable. I then use htaccess to redirect *all*...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.