PHP: Inserting source variable in the URL and emailing the value

#1. Setting a Session Variable.
A session variable is needed to hold the source value consistently throughout the site. Set the Session Variable as the very first item (line1) to load in the header (on all pages):

<?php

session_start();
$url=”http://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$query = ‘source=’;
$string = strpos($url, $query);
if ($string === false){
//what should we do if we can’t find the query?
$source = ‘no value’;
$_SESSION['s_source'] = $source;
$s_source = $_SESSION['s_source'];
}else{
// we found the query – get the source value :)
$source = $_GET['source'];
$_SESSION['s_source'] = $source;
$s_source = $_SESSION['s_source'];
}

?>

#2. Append the Source value to the navigation.
This will pass the value from page to page.

*Note: if you have the navigation broken out of the main page – make sure you start the session on navigation.php  – set this line on the very first line (line 1). <?php session_start(); ?>

<ul id=”nav”>
<li><a href=”index.php<?php echo “?source=”.$s_source; ?>” id=”nav-discover”>Discover</a></li>
<li><a href=”sample.php<?php echo “?source=”.$s_source; ?>” id=”nav-sample”>Sample</a></li>
<li><a href=”downloads.php<?php echo “?source=”.$s_source; ?>” id=”nav-downloads”>Downloads</a></li>
<li><a href=”faq.php<?php echo “?source=”.$s_source; ?>” id=”nav-faq”>FAQ</a></li>
</ul>

#3 Send the value in an email.
Collecting the value and sending it in an email.

*Note: if you have the email code broken out of the main page – make sure you start the session on contact.php  – set this line on the very first line. <?php session_start(); ?>

//In the body of the email message.
Source = $s_source

//some clarification of the Source Values:
Source is where the user came from: email blast, banner ad, etc. If this value is blank the source value is missing from the url. If this value is ‘no value’, the user did not use the correct url (missing ?source=). Both of these scenarios can happen many ways. \n

Your url will now work like the following example:

http://jaschneider.com/sample_site/index.php?source=email

OR

http://jaschneider.com/sampel_site/?source=email

Source Value = email

If you see this error: “Cannot send session cookie – headers already sent”
I would check the charset.  I’ve fixed this error with this line:
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

WordPress Themes