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

PHP Sessions

Reply
  #1 (permalink)  
Old 07-03-09, 08:22 AM
craigfarrall craigfarrall is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Sessions

Hi All,

I am working on a dating website, and I am in need with help with sessions. I have read up on this in my PHP book and the free video on here, but if anything it confused me, so was wondering if anyone could help me.

I want to be able for a user to register, and at the end of the register I would like the sessions to start and that user can then navigate through the website and it being assigned to them.

But as I said before I have tried this before, and it confuses me to be honest, so if someone can go through the basics and what I need to do/know that would be appreciated.

Thanks
Craig
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-03-09, 07:19 PM
Jcbones Jcbones is offline
Coding Addict
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 280
Thanks: 3
Thanked 5 Times in 5 Posts
PHP Code:

session_start
(); //must be called first on the page, very top before any output...^yes way up there...

//Store or call a session variable.  Yes, it is a superGlobal.
$_SESSION['variable']; 
That is the basics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-04-09, 06:33 AM
craigfarrall craigfarrall is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Ok so if I have a customer first name saved in the db as 'fname' then I can use:

Code:
$_SESSION['fname'];
Then I can echo that out later to show the fname?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-04-09, 09:44 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 2,835
Thanks: 13
Thanked 11 Times in 10 Posts
No, sessions are not the same as your database. To use variables in a session you'll need to explicitly add the variable to the session itself, like this:

PHP Code:
// place this line at top of page before any output
session_start();

//create a var with a value
$testvar "myfirstname";

// register the variable into a session
$_SESSION['testvar'] = $testvar

On another page, you can then do this (again, you need to start the session as above):

PHP Code:
// this will print nothing because the variable hasn't 
// been retrieved from the session yet
print "TESTVAR: $testvar";

// now we'll retrieve the var from the session
$testvar $_SESSION['testvar'];

// this will print the text 'myfirstname'
print "TESTVAR: $testvar"
If you have a customer name saved in the database, you must first pull the name out of the database, then store it in a session.




Quote:
Originally Posted by craigfarrall View Post
Ok so if I have a customer first name saved in the db as 'fname' then I can use:

Code:
$_SESSION['fname'];
Then I can echo that out later to show the fname?
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-05-09, 05:46 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 273
Thanks: 3
Thanked 5 Times in 5 Posts
Dont use sessions, save the username and password as a cookie.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-05-09, 08:34 PM
Jcbones Jcbones is offline
Coding Addict
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 280
Thanks: 3
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by ruteckycs View Post
Dont use sessions, save the username and password as a cookie.
Now that was alot of help.

Why not explain why you think that he shouldn't use sessions. That may be more helpful.

I would love to hear why Cookies are better than Sessions.

PS. Session is a Cookie, just not persistent.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-05-09, 09:26 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 273
Thanks: 3
Thanked 5 Times in 5 Posts
Sorry my friend I think you are confused. Sessions are NOT Cookies. Sessions are stored server side, Cookies are stored client side.

I say use cookies because as I recall for me , when I was first coding, cookies were much easier to understand and work with.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 07-07-09, 07:04 PM
Jcbones Jcbones is offline
Coding Addict
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 280
Thanks: 3
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by ruteckycs View Post
Sorry my friend I think you are confused. Sessions are NOT Cookies. Sessions are stored server side, Cookies are stored client side.

I say use cookies because as I recall for me , when I was first coding, cookies were much easier to understand and work with.

Being that a default php.ini says
Quote:
session.use_cookies specifies whether the module will use cookies to store the session id on the client side. Defaults to 1 (enabled).
And, most people will not disable that, then YES, sessions use cookies.

AT least on the 4,009,128 websites that I have visited in the past few years.

PS. 98% of my statistics are made up.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 07-06-09, 04:29 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 20 Times in 18 Posts
Cookies are about one line of code easier, but 100 times more insecure. If you care about your user's security and privacy, use sessions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 07-06-09, 02:20 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 273
Thanks: 3
Thanked 5 Times in 5 Posts
I said easier to understand not easier to code, but anyway that was for me OP may be different. As for security, I guess you have to ask yourself how likely it is someone will be attacking / routing packets for your customers computers, not likely for the home user, but for a bank or something ....?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
how to solve this PHP error? j14nhAo PHP 1 02-16-06 08:48 AM
setting PHP sessions in flash phizzlecom PHP 1 11-08-04 09:20 PM
Getting PHP to use cookies for sessions perleo PHP 1 10-24-04 08:56 PM
PHP & sessions, why won't it work? TinnyFusion PHP 1 10-04-03 02:51 PM


All times are GMT -5. The time now is 10:36 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.