Page 1 of 1

Session help

Posted: Mon Apr 23, 2007 3:19 pm
by ryanl
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I am trying to store the referring URL of customers so that when they make a purchase it connects to a page and records that URL with some other info in a text file for tracking purposes.

First, on the index page, I include a file that uses session_start(); while doing it's other functions then I have done this after the include:

Code: Select all

if (!$_SESSION['refURL']) {
$_SESSION['refURL'] = getenv('HTTP_REFERER');
}
I also tried this:

Code: Select all

if (!isset($_SESSION['refURL']) || $_SESSION['refURL']=='') {
$_SESSION['refURL'] = getenv('HTTP_REFERER');
}
Then on another page that I connect back to later on for tracking contains the following code:

Code: Select all

<?
session_start();

$refURL = $_SESSION['refURL'];
if (!$refURL) $refURL = "a direct request or unknown link";

$refId = $_SESSION[refid];
if (!$refId) $refId = "advertising or search engine";

$transId = $_GET['transId'];
if (!$transId) $transId = "not known";

$total = $_GET['total'];
if (!$total) $total = "an unknown amount of";

$output = "Transaction number $transId, totaling $total dollars was referred by $refId originating from $refURL\n";

$file = fopen("tracking.txt", "a");

fwrite($file, $output);
?>
Everything that uses GET works but the sessions are not output. When I echo them on the index they work fine but not here so I am getting the referring url correctly. Sessions are set up correctly because I use them in other places on the site.

Help!

Thanks,

Ryan


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Apr 23, 2007 3:36 pm
by arturm
you can always do:

Code: Select all

print_r($_SESSION);
on a second page to see whats inside the session.

Posted: Mon Apr 23, 2007 3:46 pm
by Christopher
Remember that you need to start the session manager before you access the session data -- that means calling session_start() on every page that uses $_SESSION. So:

Code: Select all

session_start();
if (!$_SESSION['refURL']) {
$_SESSION['refURL'] = getenv('HTTP_REFERER');
}

Posted: Mon Apr 23, 2007 6:28 pm
by ryanl
Hi,

Thanks for your replies. Artburn - I just tried this and all it prints is an empty array. Arborint - I have session_start in the included file so I didn't think that I needed to call it but I tried putting it in again just above with the same result. I don't know what could be happening. If there is nothing wrong with my code it must mean that it is getting canceled somehow but I am going straight to index.php from a link I created at localhost (for the referring url) and then I am going to track.php which is the second block of code by typing it right in the address bar. For some reason when I go to another page there is no more session. The session does work when I print it right after I create it on the index page.

Ryan

Posted: Mon Apr 23, 2007 6:45 pm
by superdezign
Is there even a $_COOKIE['PHPSESSID']?

It sounds like you don't even have sessions set up. That or you're using a very old version of PHP that requires you to use session_register(). Can you save ANY session variables?

If this is on a testing server, I'd suggest that you check your php.ini and see how it handles your sessions because your error sounds very cryptic.

Posted: Mon Apr 23, 2007 7:06 pm
by ryanl
Hi superdezign,

I have the mozilla developer toolbar. I just checked and there is a session cookie. I also realized that there are two other cookies that contain what I am looking for. I didn't know this because I didn't write the script. It is a cash crusader script which has so much code that it is mind bending. I would still like to figure out why it wasn't working but I have an inclination that it has something to do with some code somewhere in the cash crusader script but I can't figure out what. Now that I think of it, what is even stranger is that I tested this before using another session variable and it wasn't giving me any of these problems, but now testing with the same variable it is not working. Anyhow, although I didn't figure it out, your post helped me decide that I am going to do it with cookies instead. Thanks a lot, this site is great and you have all been very helpful.

Regards,

Ryan

Posted: Mon Apr 23, 2007 7:08 pm
by ryanl
By the way, I had a problem with sessions on my local server a while ago but I ended up correcting the php.ini file. This is running on a live server and it should have worked.