Jump to content


Photo

Session/Cookie


  • Please log in to reply
5 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 03:09 PM

How to a make data go into a session or cookie? Will I need a database or is it like the get/post thing?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 03:25 PM

If you want use a sesion make sure you have session_start() as the first line of any php page that uses a session. To create/access the sessioin use $_SESSION['session_variable_name_here']

So to create a session that holds your name, use:
$_SESSION['name'] = 'jwk811']
That creates a session variable called name. Which holds jwk811 as its value.

Now to use that in any page. Make sure you have session_start() at the top of the page now add:
echo $_SESSION['name'] where you want your name to appear.

The same applies with cookies however to create a cookie you must use the setcookie function. Then to access your cookie you use $_COOKIE['cookie_name_here']. NOTE: YOu can not set and use cookies at the same time like you can do with sessions.

Also when ever you use the sesison_start or setcookie function make sure you are not putting anything to the browser, otherwise you'll either get a blank screen or a nasty header cannot be sent error message.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 04 September 2006 - 03:51 PM

Also when ever you use the sesison_start or setcookie function make sure you are not putting anything to the browser, otherwise you'll either get a blank screen or a nasty header cannot be sent error message.


That can be fixed using output control.

#4 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 04:38 PM

Wow, I must be really stupid because this doesn't make any sense to me. How does everyone else get this so easily, lol. I'm trying my hardest but nothing ever works for me.  :(

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 04:43 PM

Well I havent gone into detail about using sessions/cookies. I have just brushed the surface of how to use sessions/cookies with PHP. To understand more about sessions and cookies I'd recommend that you read the manual on sessions and setcookie.

Also search google for "using sessions with PHP" and "using cookies with PHP" they should return some results.

When I started using sessions I wasnt sure either. There was a good tutorial here when I was learning sessions, but I can find it.

#6 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 04:59 PM

You see, I am trying to make a shipping cart on my website. I'm not sure but I think I'm going to need sessions to carry out the data on every page so the cart with there items in it will always be there.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users