Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2008, 02:33 PM   PM User | #1
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Question Getting the referring page in IE

I have to make a workaround for some code that basically means I need to somehow track what page has just clicked on a link to my current code. Then deal with that accordingly.

HTTP_REFERER would be perfect if the target machine wasn't running IE (which doesn't like to display this information!)

All I need is the previous pages name, or some unique way of identifying it without passing variables through.

I had toyed with the idea of using sessions, but I don't know the best way to randomly allocate and update them as a user could click on several candidate pages before clicking on the link to the code with the tracker.

Any help would be greatly appreciated.
tosbourn is offline   Reply With Quote
Old 08-28-2008, 03:21 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
HTTP_REFERER is not reliable as for example firewalls can prevent that kind of information from being sent. You'd need to use Session instead.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 08-28-2008, 03:39 PM   PM User | #3
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
It is being deployed in an enclosed environment where we can control that stuff, but that is academic since IE doesn't want to play ball!

How do I go about setting up sessions to work differently from most?

The way I see it I want one session variable, which gets updated dependent on the page you are on and can be easily tracked so I know how to act when the session variable appears.

Any ideas?
tosbourn is offline   Reply With Quote
Old 08-28-2008, 03:56 PM   PM User | #4
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Ha, turns out I was being an idiot, I know how to handle sessions fine - I was over complicating what they needed to do in my head!

[edit]

Actually no, could someone give me some pointers? I have set up my session variable but it doesn't wanna pass itself onto my page that is waiting for it.

Last edited by tosbourn; 08-28-2008 at 04:07 PM.. Reason: It would appear I am being a double idiot.
tosbourn is offline   Reply With Quote
Old 08-28-2008, 04:13 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Guess you missed to call the session_start() in the second page.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 08-28-2008, 04:18 PM   PM User | #6
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Quote:
Originally Posted by abduraooft View Post
Guess you missed to call the session_start() in the second page.
Nope, it is there in both.

A page can set a session, and once it is set it can view it, but it doesn't want to carry across to the next page.
tosbourn is offline   Reply With Quote
Old 08-28-2008, 04:21 PM   PM User | #7
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Here is the code... (just a test obviously)

Page one... (accessed first)

PHP Code:
session_start();

$_SESSION['refpage'] = "home.php"
Page two...(the thing that should handle the sessions)

PHP Code:
<?php
session_start
();
if(isset(
$_SESSION['refpage'])){
echo 
$_SESSION['refpage']; }
else{
echo 
"WHY GOD DAMN YOU, WHY?!?!?!";
$_SESSION['refpage']= "tester.php";
}
?>
The first time around the string gets printed out, the second time around (and any other times even going back to the first page) it will say tester.php (I want to to say home.php)

Last edited by tosbourn; 08-28-2008 at 04:23 PM..
tosbourn is offline   Reply With Quote
Old 08-28-2008, 09:51 PM   PM User | #8
Xiong Chiamiov
New Coder

 
Join Date: Apr 2005
Posts: 51
Thanks: 2
Thanked 0 Times in 0 Posts
Xiong Chiamiov is an unknown quantity at this point
I don't have that problem at all; it acts just as expected. Unless I move the ="tester.php" code outside of the else loop, it never gets executed, and if it does, resetting the value to "home.php", well, resets it.

I'd take a look at your cookie settings. Make sure that above is really *all* you've got in your test pages, and try different browsers.
Xiong Chiamiov is offline   Reply With Quote
Old 08-28-2008, 10:48 PM   PM User | #9
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Page two is 100% just that, page 1 has a load more stuff on it.

Basically this is a project I have been assigned that someone else had completed but not properly - I have to fix it up (including making it IE proof).
tosbourn is offline   Reply With Quote
Old 08-29-2008, 11:01 AM   PM User | #10
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
Here is sessions.php - one of the things called on that first page before I declare my session. This is the only thing that I would think would come close to affecting sessions.

PHP Code:
<?php 
require_once("dbfactory.php");
define("STAMP_FORM""YmdHis"); // Default timestamp format.

function sess_connectDB(){
    
/*
        function to connect to the backend database with local db defined parameters.
    */
    
try  {
        
$dbh = new DBFactory(); 
        return 
$dbh;
    }
    catch(
ADODB_Exception $e){
        
header("Location: nodb.php");
        exit;
    }
}
function 
sopen($save_path$session_name){    
    
/*
        No need to do anything here.
    */
    
return true;
}
function 
sclose(){
    
/*
        No need to do anything here.
    */
    
return true;
}
function 
sread($id){
    
/*
        Reads session data from the database
    */
    
$dbh sess_connectDB();
    
$sql "SELECT * FROM `TS::Sessions` WHERE sid='$id'";
    
$dbh->SetFetchMode("ASSOC");
    
$rs $dbh->Execute($sql);
    
$row $rs[0];//->fields;    
    //print_r($row);
    
return $row['sdata'];
}
function 
swrite($id$sess_data) {
    
/*
        Writes session data to the database
    */
    
try {
        
$dbh sess_connectDB();
        
$clean_data $dbh->qstr($sess_data);
        
$timestamp date(STAMP_FORM);
        
$sql "DELETE FROM `TS::Sessions` WHERE sid='$id'";
        
$dbh->Execute($sql);
        
$sql "INSERT INTO `TS::Sessions` (sid, sdata, modtime) VALUES('$id', $clean_data, '$timestamp')";
        
$dbh->Execute($sql);
        
//printr($sql);
    
}
    catch (
Exception $e){
        
var_dump($e);
    }
}
function 
sdestroy($id){
    
/*
        We are finished with the db, so wipe the session
    */
    
$dbh sess_connectDB();
    
$dbh->Execute("DELETE FROM `TS::Sessions` WHERE sid='$id'");
    
$_SESSION= array();
}
function 
s_gc($maxlifetime){
    
/*
        Garbaage collection.
    */
    
$dbh sess_connectDB();
    
$ts time() - $maxlifetime;
    
$timestamp date(STAMP_FORM$ts);
    
$dbh->Execute("DELETE FROM `TS::Sessions` WHERE modtime < '$timestamp'");
}

// Make PHP use *our* session handling.
if (session_set_save_handler
  
"sopen"
  
"sclose"
  
"sread"
  
"swrite"
  
"sdestroy"
  
"s_gc" 
)===false){
    die(
"Sessions are broken");
}

// As this will be included in any page that wants session access, we might as well start it here.
session_start();
But I can't see anything in it that would stop you making your own sessions (he has used them in the code and I am assuming they are doing their job!!)

Any help would be greatly appreciated!
tosbourn is offline   Reply With Quote
Old 08-29-2008, 02:18 PM   PM User | #11
DaiWelsh
New Coder

 
Join Date: Aug 2003
Location: Derby, UK
Posts: 97
Thanks: 0
Thanked 14 Times in 14 Posts
DaiWelsh is an unknown quantity at this point
The original coder has replaced default session storage with their own database solution, this means that any session data created in the first page will not be stored in the normal PHP session location and hence will not be available to your second page _unless_ it also includes this code and hence shares the replacement session storage.

The solution is either to comment out the replacement session handling throughout (but there may be a very good reason why it does this) or to include the same session.php in your second (test) page and anywhere else that you want to handle session data.

HTH,

Dai
DaiWelsh is offline   Reply With Quote
Users who have thanked DaiWelsh for this post:
tosbourn (08-29-2008)
Old 08-29-2008, 02:58 PM   PM User | #12
tosbourn
Regular Coder

 
Join Date: Aug 2008
Location: Northern Ireland
Posts: 167
Thanks: 12
Thanked 6 Times in 6 Posts
tosbourn is on a distinguished road
I had just stuck the sessions thing in about 15 minutes before reading this! Worked a treat.

Thanks so much!
tosbourn is offline   Reply With Quote
Reply

Bookmarks

Tags
http_referer, internet explorer, page, tracking

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:21 AM.


Advertisement
Log in to turn off these ads.