Current location: Hot Scripts Forums » Programming Languages » PHP » Cookie VS Sessions

Cookie VS Sessions

Reply
  #1  
Old 07-10-05, 09:25 AM
Lafa's Avatar
Lafa Lafa is offline
Newbie Coder
 
Join Date: Jul 2005
Location: Moss, Norway
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Cookie VS Sessions

What's the pro's and con's for each of them?
__________________
TUXRocks, you dont.

Push it to the limit
Reply With Quote
  #2  
Old 07-10-05, 01:13 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Not Cookies VS Sessions, but Cookies + Sessions is the best way to go..
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #3  
Old 07-10-05, 01:23 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
sessions are temporarily held on the server and cookies are permanently stored on the users computer (unless deleted) and sessions are harder to manipulate than cookies...
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #4  
Old 07-11-05, 04:52 AM
Hibame Hibame is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by FiRe
sessions are temporarily held on the server and cookies are permanently stored on the users computer (unless deleted) and sessions are harder to manipulate than cookies...
Which I belive basicly means sessions are more secure but cookies last longer
Reply With Quote
  #5  
Old 07-11-05, 09:21 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
yes, in a way...
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #6  
Old 07-11-05, 11:58 AM
Lafa's Avatar
Lafa Lafa is offline
Newbie Coder
 
Join Date: Jul 2005
Location: Moss, Norway
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Acecool, FiRe and Hibame: thanks for your help sinc i know atleast a bit about sessions il continue with that for now

oh and if you got some nice tut's on how to do sessions/cookies i would love it, im still very basic.
__________________
TUXRocks, you dont.

Push it to the limit
Reply With Quote
  #7  
Old 07-11-05, 01:00 PM
DetroitGuy DetroitGuy is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
I just switched my entire website to sessions last night. Sessions aren't hard to work with, but it can be a bit of a task when you are switching over to sessions.

http://www.phpbuddy.com/article.php?id=14

That is the most basic and most helpful tutorial I found on sessions.
Reply With Quote
  #8  
Old 07-11-05, 02:12 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 2,838
Thanks: 13
Thanked 11 Times in 10 Posts
Quote:
Originally Posted by Lafa
oh and if you got some nice tut's on how to do sessions/cookies i would love it, im still very basic.
This one is fairly clear; I don't remember where I got it though.

================================================== ====

This tutorial will teach an alternative and effective solution to cookies in PHP which might actually be better for your website and security.

A session is defined in PHP and throughout the Internet as a unique visit to a particular website and it's subsidaries. How can sessions in PHP help you out? Well, let's say you have a dynamic website where you want to have a person sign in with a username and password. Once he's in, you want him to be able to access all parts of your website using that name and password.

There are several ways to "remember" his username and password while he's at your site. One way is to use cookies. The advantage of using cookies is that once he logs in, the cookie stores the visitors information on that computer for as long as the duration of the cookie, even if the session is over. For example, if he logs in and browses around your site and then closes IE, and comes back tomorrow to your page, it'll still remember his name and password. The obvious disadvantage of cookies is that it's a security hazard. Also, some people have cookies disabled so it may not be a viable solution. PHP Sessions are a safer, always working method of storing variables in PHP throughout the duration of the visitors stay.

Alright, let's get started. The first thing you have to place is:
<?
session_start();
?>
The session_start() has to be placed on the every page you want these variables to follow along witht the user. Once you have started the session, to add variables to the session, all you have to do is use the _Session varible. For example, if you want to have a username variable with a value of "Bobert", you write:
<?
$_SESSION["username"] = "Bobert";
?>
Now, just think of $_SESSION["username"] as any other variable like $username. You can do anything you want with it, and it'll follow around your website from one page to another. A session is ended whenever the visitor leaves your site, if you ever want to destroy/kill a variable inside the session, just use this command:
<?
unset($_SESSION["variable"]);
?>
and replace variable, with the name of the variable you want to delete. If you want to end the session all together while still keeping the visitor on your site, use:
<?
session_destroy();
?>
That logs off the user. I hope this tutorial helped you in realizing an alternative, safer way PHP has come up to deal with site global variables than cookies.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Reply With Quote
  #9  
Old 07-11-05, 05:48 PM
Lafa's Avatar
Lafa Lafa is offline
Newbie Coder
 
Join Date: Jul 2005
Location: Moss, Norway
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
as i said above, i know how the basic of both cookies and session worksa:

for an instant if you a match was made in db:
PHP Code:
$row $db->fetch_array("SELECT * FROM user WHERE user_auth='$username' AND user_first_name='$first_name'");

if(
$row) {
    
$_SESSION['username'] = $row[0]
    
$_SESSION['firstname'] = $row[1]

like that, but well thats what i know about session.

and not pretty much the same about cookie's so if you got something more advacne than that, it would help

thanks for all the reply's tho
__________________
TUXRocks, you dont.

Push it to the limit

Last edited by Lafa; 07-11-05 at 05:50 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie problem Tempestshade PHP 5 06-14-05 06:37 AM
sessions not working, please help! tallpaul858 PHP 2 04-29-05 07:23 AM
Setting a cooke (time it expires) mcrob PHP 4 04-27-05 01:13 PM
Passing info between pages w/ URL HairySpider JavaScript 6 01-09-05 09:47 AM
Weird behaviour with sessions Skeleton Man PHP 1 10-27-04 10:42 PM


All times are GMT -5. The time now is 07:11 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)