<?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>Perplexed Labs &#187; sessions</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/sessions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Thu, 09 Sep 2010 17:11:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP Sessions on localhost</title>
		<link>http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/</link>
		<comments>http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 19:08:21 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[localhost]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=116</guid>
		<description><![CDATA[Trying to setup a sandbox development environment on my laptop before traveling for the holidays.  Found WAMP which was a breeze to setup and get running. Anyway, I ran into a problem where basic session code that my servers handled fine wasn't working as expected when accessed via localhost.  This is how I got things [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/10/05/php-custom-session-handler/' rel='bookmark' title='Permanent Link: PHP Custom MySQL Session Handler'>PHP Custom MySQL Session Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Trying to setup a sandbox development environment on my laptop before traveling for the holidays.  Found <a title="WAMP" href="http://www.wampserver.com/en/" target="_blank">WAMP</a> which was a breeze to setup and get running.</p>
<p>Anyway, I ran into a problem where basic session code that my servers handled fine wasn't working as expected when accessed via localhost.   This is how I got things working:</p>
<p>Set ALL session configuration parameters BEFORE calling session_name() (which, additionally, should ALWAYS be called before session_start(), fyi).  This includes, most importantly, session cookie parameters.  The following is one example that would work for either localhost or operating on a production server:</p>
<pre class="brush: php;">
function domainName()
{
	$serverName = $_SERVER['SERVER_NAME'];
	$serverNameParts = explode('.', $serverName);
	if(count($serverNameParts) &lt; 2) {
		return $serverName;
	} else {
		return $serverNameParts[count($serverNameParts) - 2].'.'.
			$serverNameParts[count($serverNameParts) - 1];
	}
}

function cookieDomainName()
{
	$domain = domainName();

	// if we're operating on localhost, provide a blank domain
	// otherwise cookies won't be set
	return ($domain == 'localhost') ? '' : '.'.$domain;
}

session_set_cookie_params(0, '/', cookieDomainName());
session_name('mySessionName');
session_start();
</pre>
<p>Hope this helps!</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/10/05/php-custom-session-handler/' rel='bookmark' title='Permanent Link: PHP Custom MySQL Session Handler'>PHP Custom MySQL Session Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2008/12/21/php-sessions-on-localhost/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
