Jump to content

Recommended Posts

I have this code for referer redirect:

 

<?

$referrer = $_SERVER['HTTP_REFERER'];

if (preg_match("/outsidesite.com/",$referrer)) {

header('Location: http://www.mysite1.com');

}

else {

header('Location: http://www.mysite.com');

};

?>

 

But the question is:

If I want to match a LIST OF SITES stored on a txt file, insted of "outsidesite.com".

How can I work with a txt list of sites: "mysites.txt"

instead of only one site: "outsidesite.com"

 

<?

$referrer = $_SERVER['HTTP_REFERER'];

if (preg_match("HERE A TXT WITH A LIST OF SITES mysites.txt",$referrer)) {

header('Location: http://www.mysite1.com');

}

else {

header('Location: ht*tp://www.mysite.com');

};

?>

 

 

Anyone knows what to put here: ("HERE A TXT WITH A LIST OF SITES mysites.txt"), to make it functional.

 

 

I am working with this:

 

$file=file_get_contents("file.txt");

 

if (preg_match ("/$file/", ...........

 

But it only retrives the first line in the txt file, not the second line.

 

thank you.

 

Link to comment
https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/
Share on other sites

i think this is what your looking for..

 

but just so you know

'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.

 

 

<?php

/* File example
http://site1.com,http://site2.com,http://site3.com
*/
$file=file_get_contents("file.txt");
$sites = explode(",",$file); // change "," to "\n" for line delimitors

$referrer = $_SERVER['HTTP_REFERER'];
foreach($sites as $site)
{
	if (preg_match("%$site%i",$referrer))
	{
		// found
		header('Location: http://www.mysite1.com');
		exit;
	}
}
//Not found
header('Location: http://www.mysite.com');
exit;
?>

Thank you for your answer.

 

I am testing this (I added some missing "}"):

 

<?php

$file=file_get_contents("file.txt");
$sites = explode("\n",$file); // change "," to "\n" for line delimitors

$referrer = $_SERVER['HTTP_REFERER'];
	foreach($sites as $site)
{
	if (preg_match("%$site%i",$referrer))
	{
		echo "works";
		exit;
	}
else {
//Not found
echo "dont work";
exit;}
}
?>

 

And my file.txt is:

 

site1.com
site2.com
site3.com

 

but only works for site1.com not for site2.com or site3.com.

I dont know what I am doing wrong.

Thank you for your help.

 

file_get_contents returns the entire contents of the file in a single string. you probably want to use file() which returns an array with each line of the file as an element.

 

Thank you.

I testing this code:

 

<?php

$file=file ("file.txt");
$sites = explode("\n",$file); // change "," to "\n" for line delimitors

$referrer = $_SERVER['HTTP_REFERER'];
	foreach($sites as $site)
{
	if (preg_match("%$site%i",$referrer))
	{
		echo "works";
		exit;
	}
else {
//Not found
echo "dont work";
exit;}
}
?>

 

But the same problem.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.