Jump to content

Session/Cookie


jwk811

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.