I need a php script that verifies parent website!!

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!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

I need a php script that verifies parent website!!

Post by cap2cap10 »

:banghead: I need a simple php script that verifies that a user came from my parent website and not from any other site.
If the user did not come from the parent site then they should be redirected to the home page to login.

Any php technorati out there who knows how to do this?


Thanks in advance,

Batoe
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: I need a php script that verifies parent website!!

Post by cap2cap10 »

ok, how's this:

Code: Select all

<?php
 
$foobar = getenv('HTTP_REFERER');
 
function check_previous()
{
 if ($foobar != 'www.yoursite.com')
  {
Header('Location: http://www.yourhomepage.com');
  }
 
}
?>


Let me know if I did something wrong.

Batoe
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: I need a php script that verifies parent website!!

Post by onion2k »

Moved to PHP - Code folder.

Your script there has a number of problems.

1. The scope of the $foobar variable means it won't be visible inside your function, so every time you call the function you'll end up redirecting the user.

2. The function doesn't strip off the protocol or the file path from the referer. You'll need to remove the http:// or https:// bit from the front and any file like index.php or users/username/ from the end.

3. Sending the refererer is optional. Some users will have come from the parent site yet HTTP_REFERER will be empty. You'll be redirecting people even though they're not meant to be redirected.
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: I need a php script that verifies parent website!!

Post by cap2cap10 »

ok,

1. The scope of the $foobar variable means it won't be visible inside your function, so every time you call the function you'll end up redirecting the user.

How do I narrow the scope to make sure the function will recognize the previous url?

Batoe
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: I need a php script that verifies parent website!!

Post by onion2k »

This will tell you about variable scope: http://uk2.php.net/variables.scope

But you really need to read this: http://uk2.php.net/manual/en/functions.arguments.php
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: I need a php script that verifies parent website!!

Post by cap2cap10 »

Ok,

I have included the variable within the function {};

2. The function doesn't strip off the protocol or the file path from the referer. You'll need to remove the http:// or https:// bit from the front and any file like index.php or users/username/ from the end.

What If I need the user to come from a specific subdirectory, can I keep the "users/username/" for example?

Batoe
Post Reply