Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,687 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,271 people online right now. Registration is fast and FREE... Join Now!




checking refferal page

 
Reply to this topicStart new topic

checking refferal page

musya
post 31 May, 2008 - 02:25 PM
Post #1


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


Is there a way to see if a certain page referred you to it?

For example I have a login script and it goes to a page that validates them and such but i dont want direct access to that page allowed, meaning that, the page will check to see if it was accessed by index.php which contains a form that sends the user to it. Does anybody know? or follow me?

Thank you.
Musya
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:21 PM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


you would need to add this to your site:
php
$ref=@$HTTP_REFERER; 


I would then suggest exploding the result and comparing it in an if, else statement... since it's possible to go through http://site.com OR through http://www.site.com, the if needs to contain "http://site" and "site".

php
<?php
$ref=@$HTTP_REFERER;
$ref = explode(".",$ref);
if($ref[0] = "http://site"||$ref[1] = "site"){/*code to be executed*/}
else{/*code to be executed*/}


I just threw this together quickly, so it might need some tweaking, but it should work =)

i made one in JS earlier for outgoing links, so it's very similar.

This post has been edited by JBrace1990: 31 May, 2008 - 04:22 PM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 May, 2008 - 04:26 PM
Post #3


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well first if you design the validation page correctly you can make it check for the username and pass and if not provided, simply die or redirect back to the sign in screen.

That would be my first choice and is very secure. But to answer your point more directly yes, there is a way to check the "referrer" that referred the user to the validation page.

It is in the super global array as $_SERVER["HTTP_REFERER"]. But as the PHP.net page says, and you should already know, this can't be truly relied on. People could directly type in the URL of the validation page and not have a referrer. But in most cases this variable will be populated with the referring page.

Just use with caution and again, if you simply test that the user provided a username and pass, it really shouldn't matter where that data comes from... as long as it is validated for correctness and puts the user where they need to go on success or failure.

I hope that helps you out! Enjoy!


Edit: Just want you to know that $HTTP_REFERER has been deprecated for a long time and $_SERVER["HTTP_REFERER"] be used in its place.

smile.gif

This post has been edited by Martyr2: 31 May, 2008 - 04:28 PM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 04:29 PM
Post #4


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(musya @ 31 May, 2008 - 03:25 PM) *

Is there a way to see if a certain page referred you to it?

For example I have a login script and it goes to a page that validates them and such but i dont want direct access to that page allowed, meaning that, the page will check to see if it was accessed by index.php which contains a form that sends the user to it. Does anybody know? or follow me?

Thank you.
Musya


There a couple of ways of doing this. The first is that you could try to use the $_SERVER['HTTP_REFERER'] variable.

CODE

<?php

$lastpage = $_SERVER['HTTP_REFERER'];

if ($lastpage != "index.php") {
    header("Location: needpermission.php");
}

// The rest of your page goes here

?>


Be sure to put that at the top of your apge. needpermission.php is the page that you to redirect to. HTTP_REFERER doesn't always work though, so it can be very unreliable. You may want to set a session variable on the index page. If so, on your index page put the following

CODE

<?
session_start();
$_SESSION['indexAccessed'] = true;

// The rest of your index pages code

?>


Then at the top of your processing processing page you would put

CODE


<?php
     if ( ! ($_SESSION['indexAccessed']) ) {
          header("Location: redirectpage.php");
     }

     $_SESSION['indexAccessed'] = false;

    // Process the form
?>



Hope that helps. Let me know if you need any more assistance.
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 2 Jun, 2008 - 06:48 PM
Post #5


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


If all you need to do is make sure that the referrer contains a certain keyword/domain, you could always use a regular expression, too - I had a guy linking to one of my sites that I didn't want linking to my site, and this is what I used to redirect all requests from his site to my pretty pony:
php

<?php
if(preg_match('/yourwordhere/',$_SERVER['HTTP_REFERER'])) {
header('Location: http://www.hasbro.com/mylittlepony/');
}
?>

...And you'd just replace 'yourwordhere' with whatever you wanted to be in their referrer - that could be a specific query string parameter, a URL, or even just a www - whatever you want.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:20AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month