开发者

referring url in php

I am trying to setup my 404 page to开发者_C百科 detect the referring url. I would like to this in PHP any ideas? thank you very much


.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

Now, all your requests will be redirected to index.php The requested uri segment will be available as $_SERVER['REQUEST_URI']. So just take that and look up that string in your db and do whatever you need to do with that.


Example: I set up a test folder in my localhost environment, added the .htaccess and created a new index.php

<?php
$uri = $_SERVER['REQUEST_URI'];
$segments = explode('/', $uri);
var_dump($segments['2']);

Sent your browser to: http://localhost/test/123/foo and it will print

string '123' (length=3)

Now you have your string in $segments['2']. Use that in your db query. As easy as that.


<?php
$referer = $_SERVER['HTTP_REFERER'];
?>

Note: Don't rely too much on this data as easily can be faked.

In your apache2.conf (or .htaccess)

ErrorDocument 404 /your_error_dir/your_error_file.php

Update:

http://ex.am/c8N4j

Your mod_rewrite should detect the given parameter: c8N4j Your PHP script should search the given parameter in your database and if it's found it should redirect user to the actual page.


did you mean?

$_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.it cannot really be trusted.

Take a look also at Determining Referer in PHP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜