Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

Session/Cookie

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 [url=http://php.net/setcookie]setcookie[/url] 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.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=106847.msg427790#msg427790 date=1157383516]
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.
[/quote]

That can be fixed using [url=http://www.php.net/manual/en/ref.outcontrol.php]output control[/url].

Share this post


Link to post
Share on other sites
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.  :(

Share this post


Link to post
Share on other sites
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 [url=http://php.net/session-start]sessions[/url] and [url=http://www.php.net/setcookie]setcookie[/url].

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.