
07-03-09, 08:22 AM
|
|
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
|

07-03-09, 07:19 PM
|
|
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.
|

07-04-09, 06:33 AM
|
|
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?
|

07-04-09, 09:44 AM
|
 |
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
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)
|

07-05-09, 05:46 PM
|
 |
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.
|

07-05-09, 08:34 PM
|
|
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
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.
|

07-05-09, 09:26 PM
|
 |
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.
|

07-07-09, 07:04 PM
|
|
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
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.
|

07-06-09, 04:29 AM
|
 |
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.
|

07-06-09, 02:20 PM
|
 |
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 ....?
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|