PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
<?
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.
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]
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:
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.
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.
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.
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.