<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webcodez - The database of web programming tutorials &#187; session_start</title>
	<atom:link href="http://www.webcodez.net/tag/session_start/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Tue, 18 May 2010 16:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Sessions &amp; Cookies</title>
		<link>http://www.webcodez.net/php-mysql/php-sessions/</link>
		<comments>http://www.webcodez.net/php-mysql/php-sessions/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 09:34:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[$_SESSION]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SESSIONS]]></category>
		<category><![CDATA[session_start]]></category>
		<category><![CDATA[set_cookie]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=145</guid>
		<description><![CDATA[In PHP special variables called SESSIONS can be used to store data into for a longer period ( a session ). Session variables can as well be used on different pages of your website ( any page ). They can for example be used to save a login session which has to be remembered for [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP special variables called <strong>SESSIONS </strong>can be used to store data into for a longer period ( a session ). Session variables can as well be used on different pages of your website ( any page ). They can for example be used to save a login session which has to be remembered for a longer period ( until the user logs out ) and on all pages of the website.</p>
<p>A session variable can be created like this:</p>
<pre name="code" class="php:nogutter">$_SESSION['variable_name'] = "value";</pre>
<p>So it&#8217;s just like the POST variables, an array which contains ( in this case ) all SESSION variables. To be able to use session variables on a page, you need to first use the session_start function on the top of the page ( before any (HTML/graphical) output ).</p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
... rest of your page (SESSION variables enabled) ...
?></pre>
<p>An example of a session variable which we can use on any page again:</p>
<p><strong>File: create_session.php</strong></p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
$_SESSION['username'] = "Webcodez";
?></pre>
<p>File: example.php</p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
echo "Hey ".$_SESSION['username'];
?></pre>
<p>We could also make a page which unsets the session variable &#8216;username&#8217; (logs the user out for example). We use the function <strong>unset </strong>to do this.<br />
 <br />
<strong>File: example.php</strong></p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
unset($_SESSION['username']);
?></pre>
<p>You could combine this with POST variables, to make the user choose an username to be logged in with for example:<br />
<strong><br />
File: create_session.php</strong></p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
if($_POST) {
    $_SESSION['username'] = $_POST['username'];
    header("location: example.php");
}else{
?>
<form metod="POST" action="&lt;?php echo $_SERVER['PHP_SELF']; ?>">
<input type='text' name='username'>
<input type='submit' name='submit'>
</form>

&lt;?php
}
?></pre>
<p>File: example.php</p>
<pre name="code" class="php:nogutter">&lt;?php
session_start();
If($_SESSION['username']) {
    echo "Hi there ".$_SESSION['username'];
}else{
    header("location: create_session.php");
}
?></pre>
<p>The function session_start is again used in all pages to be able to create and use sessions. For the rest we only create the session $_SESSION['username'] and use a form and POST variables to save the user input into the session variable $_SESSION['username']. The header(location: location_here) is used to make it go to another page automaticly. If you aren&#8217;t familiar with the POST variables and form, you should have a look at Chapter 9 – POST variables.</p>
<p>NOTE: You can also use the function <strong>session_destroy</strong> to simply unset (destroy) ALL session variables. &#8211; <strong>session_unset()</strong> seems to work as well for unsetting the whole $_SESSION array.</p>
<p>Instead of using Sessions, you could also use Cookies to store data into for a longer period ( couple of days for example, anything ).</p>
<pre name="code" class="php:nogutter">
set_cookie("my_cookie", "my_cookie_value", ... timestamp untill which the cookie stays remaining ... , ... path on which the cookie should work ... );
</pre>
<p>You see we use the function set_cookie to create a cookie variable. In this example we called it &#8220;my_cookie&#8221; so it&#8217;d be $_COOKIE['my_cookie'] and we gave it the value &#8220;my_cookie_value&#8221;. So it created $_COOKIE['my_cookie'] = &#8220;my_cookie_value&#8221;, which however can not be set in this way ( as we done it for SESSION variables ) &#8211; the set_cookie function is used as we need to set more parameters. The time we want the cookie to last ( better said: the expiration time ) for example. This needs to be written in timestamp (the UNIX timestmap) which is in seconds. The function time() gives the current timestamp in seconds, so time() + 3600, would make a cookie stay for 3600 seconds more and then it exires ( which is 1 hour more ). The path can be anything, say you want to have the cookie work on all paths of your website, then you use &#8220;/&#8221; as path.We create one cookie that works on all paths of the website for 1 hour:</p>
<pre name="code" class="php:nogutter">
set_cookie("my_cookie", "my_cookie_value", time() +3600 , "/" );
</pre>
<p>To delete a cookie, you simply make it expire:</p>
<pre name="code" class="php:nogutter">
set_cookie("my_cookie", "", time() -1, "/"); //-1 could be anyhing, "" could be anything aswell as it will expire anyway so no matter what value you give it
</pre>
<p>Now we set the expiration time to 1 second before the current time. You could set it any time before the current time, just so it would expire right now.</p>
<p>To check whether a sesson or cookie exists, you can use the isset function. Like:</p>
<pre name="code" class="php:nogutter">
if( isset($_SESSION['my_session']) OR isset($_COOKIE['my_cookie']))
</pre>
<p>Cheers,<br />
Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-sessions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
