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: 358
Thanks: 4
Thanked 15 Times in 15 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: 3,030
Thanks: 14
Thanked 34 Times in 33 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: 377
Thanks: 6
Thanked 10 Times in 10 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: 358
Thanks: 4
Thanked 15 Times in 15 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: 377
Thanks: 6
Thanked 10 Times in 10 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-06-09, 04:29 AM
Nico's Avatar
Nico Nico is online now
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,563
Thanks: 5
Thanked 25 Times in 23 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
  #9 (permalink)  
Old 07-06-09, 02:20 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 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
  #10 (permalink)  
Old 07-07-09, 08:42 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,030
Thanks: 14
Thanked 34 Times in 33 Posts
Quote:
Originally Posted by ruteckycs View Post
As for security, I guess you have to ask yourself how likely it is someone will be attacking / routing packets for your customers computers
That's not the question you should be asking. What you should be asking is, "Do I want to code this securely or not?"

Cookies are pretty easy to exploit, and although you may not care about the data or think it's worth hacking, someone else might. Oftentimes hackers go for the "low-hanging fruit" (the easy stuff), so why make it any easier for them?

I often hear the argument that "this data isn't important" or "this data isn't worth anything". In the first instance, it may not be important to you, but chances are it's important to somebody.

In the second instance, it's not necessarily the value of the data itself, but the access that cracking the data can bring, like gaining access to your server or user accounts, thereby creating an opening that can be further exploited.

Saying that "nobody wants this data" is like saying that "nobody wants your front door", so why not just make it out of cardboard. It's not the door that's important, it's the fact that it keeps people out of your home.

Anytime I hear people coming up with reasons not to code securely, I just shake my head. It's like trying to justify not wearing a seatbelt when you drive: "No one wants to hit my car."

On the other hand, I really should thank the insecure coders of the world, because it means that hackers will be targeting them instead of me. And I'm okay with that.


Quote:
Originally Posted by ruteckycs View Post
not likely for the home user, but for a bank or something ....?
Honestly, you'd be surprised how often home networks and end user PCs are targeted.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)

Last edited by End User; 07-07-09 at 08:45 AM.
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 12:11 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.