/    Sign up×
Community /Pin to ProfileBookmark

How to change a variable in a php file based on a referrer page?

Umm.. hi.

I’m trying to make something that will change a variable ($variable) set in a php file (other.php) based on a match of keywords from the refferer (index.php?a=X) from which the guest came to the page.

For example, I have this index.php?a=one, and index.php?a=two. Each of them, when clicked will change the location.href to ‘other.php’, and on ‘other.php’ I have this:
echo ‘<a href=”index.php?a=’.$variable.'”>clicky</a>’;

How exactly can I set $variable to be either ‘one’ or ‘two’ based on what referrer I used to go to other.php ? :-/

(maybe there’s a way simpler solution and I’m terribly blind)

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@chestertbMay 13.2007 — Are you asking is "How do I read the values in the variables submitted to a php page?"

If so, then you use one of $_GET[], $POST[] or $_REQUEST[]

<?php

//in the php script receiving the data

$variable = $_GET['a'];

?>
Copy linkTweet thisAlerts:
@DeltaOneauthorMay 13.2007 — Umm, let me get this straight..

I want on the 'other.php' page to set the '$variable' content to either 'one' or 'two' based on the page that the user used to come to 'other.php', aka 'index.php?a=one' or 'index.php?a=two' because, as shown below, I want to echo an <a href=""> with basically a link that will take back the user to the previous page, aka 'index.php?a=one' or 'index.php?a=two'

Complicated to explain..
Copy linkTweet thisAlerts:
@chestertbMay 13.2007 — Oh, I see. Sorry. I misunderstood your question.

First, this from the php manual...

[I]$_SERVER['HTTP_REFERER '] - The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. [/I]

With that in mind, in other.php...

$variable = $_SERVER['HTTP_REFERER'];

This will give you the whole address of the page from which the user came to your page. Your HTML would then just be something like

[code=php]if($variable!="")
{
?>
<a href="<?=$variable?>">GO BACK</a>
<?php
}[/code]


CTB
Copy linkTweet thisAlerts:
@DeltaOneauthorMay 14.2007 — Thanks alot!
Copy linkTweet thisAlerts:
@DeltaOneauthorMay 14.2007 — Okai, this is very confusing.

Can someone help me draft this php thinglet? It's futile for me..


I have this index.php file. If I'm on index.php?a=one&p=4 and on index.php?a=two&p=4 the browser will direct me to other.php.

(Header("Location: other.php")?


Now, on other.php I have:

$variable = $_SERVER['HTTP_REFERER'];

echo '<a href="'.$variable.'">Back</a>';

And it doesn't do anything, as if the server (or browser) can't forward any referer (blank)

ANYWAY, I just want to make some little thingy that when put on other.php page, it will echo the <a href= with the link back to the page that was used to access other.php... :-/
Copy linkTweet thisAlerts:
@sitehatcheryMay 14.2007 — One sure fire way:

[code=php]
$clean['p']=ctype_digit($_GET['p']);

switch($clean['p']){
case 1: header('location: page1.php');
case 2: header('location: page2.php');
case 3: header('location: page3.php');
default: header('index.php');
}
[/code]
Copy linkTweet thisAlerts:
@chestertbMay 14.2007 — It could be, as per the warning from the php manual, that you're not receiving the refering page.

It's time to do some debug testing to see what you're getting.

other.php should look like this...

[code=php]<?php
//collect the referer address
$referer = $_SERVER['HTTP_REFERER'];

//draw a page
?>
<html>
<head><title>Test Referer</title></head>

<body>
<p>Variable Is: <?=$referer?></p>
<p><a href="<?=$referer?>">Back To <?=$referer?></a>
</body>
</html>[/code]


That will tell you what value you're receiving.

Note that I've switched off the php parser to output html, and then use the <?= ?> notation to isert my php variables into the html script. It's just a personal preference, but I find it easier, less prone to syntax error, and therefore easier to debug.

I hope this helps

CTB
Copy linkTweet thisAlerts:
@DeltaOneauthorMay 14.2007 — Mmm, does that one work for you?

Apparently I'm not receiving the reffering page, although it's quite funny, as I've tested it on two different browsers and afterwards on two different locations (localhost, and on live server). - all I get is ""

The above piece of code does work for you, no? I don't see anything wrong with it, but it fails to work here :-/ And yeah, that might be easier and less prone to syntax errors, but it takes a while to get used to :p
Copy linkTweet thisAlerts:
@TaschenMay 14.2007 — You'll need to pass some values if your sending a header/location as referrer won't work (I think)...

Try this:

$clean['p']=ctype_digit($_GET['p']);

Header("Location: other.php?page=index&ref=$clean['p']")

And the use $_GET['ref'] and $_GET['page'] to access the page and page resource to build your link with.
Copy linkTweet thisAlerts:
@chestertbMay 15.2007 — Taschen, that assumes you have control over the referrer page. When you don't, and you still want to send your visitor back to where they came from, $_SERVER['HTTP_REFERER'] should work, unless the user agent isn't sending it.

And DeltaOne, if you run that script directly from the address line in your browser, there is no referer. You need to link to it from another page. Then you should see that page name if $_SERVER['HTTP_REFERER'] is working.
Copy linkTweet thisAlerts:
@DeltaOneauthorMay 15.2007 — True enough, I tested it with two files, a to_variable.html and a variable.php.

On to_variable.html I have a little <a href="variable.php">clicky</a>, and on variable.php that php script. And it ain't working. :-/


I'm somewhat interested in what Taschen is saying too, as I do have control of the referrer page.. something like that might work here, ALTHOUGH this simple referrer thingy would have done wonders..

So, on index.php?a=one&p=4 I'll use the

$clean['a']=ctype_digit($_GET['a']);

header("location: other.php?t=$clean['a']")

and on other.php I'll simply add a

if isset $t, if $t == 'one' or if $t == 'two' and change the a href based on what $t is.. it might work..
×

Success!

Help @DeltaOne spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...