Thanks for the welcome, Brook.
OK, as you're using this for urls posted inside someone's post on your forum, you'll need to use whatever db field your forum software uses for that, and change that in the script to reflect that.
This redirect assumes you are using a db table with the following fields (change to suit your needs and purposes)
ID (primary, auto_increment, unique)
url (varchar 255)
Create a file on your server that will be used in the case that someone tries to pass a url through your redirect script that is not permitted by you. Call this file denied.php
Inside denied.php put any text in there you wish to inform the user that they do not have authorization to use your redirect script for the domain they tried to attach to redir.php
1. Create the file and call it db_inc.php.
2. Copy and paste the following text (located below) into the db_inc.php file.
3. Replace db_user with your db username.
4. Replace db_pass with your db password.
5. Replace db_name with the name of your database.
6. Place db_inc.php inside whatever folder you use on your server to store include files.
Code:
<?php
$connection = @mysql_connect("localhost" , "db_user" , "db_pass") or die (" Could not connect to the server. ");
$db = @mysql_select_db("db_name" , $connection) or die (" Could not select database. ");
?>
1. Create the file called redir.php
2. Inside redir.php copy and paste the code below into the file.
3. Replace /path/to/db_inc.php -- with your root path to the file.
4. Replace db_table with the name of your db table.
5. Replace domain_name.com with the name of your domain.
Code:
<?php
@require_once("/path/to/db_inc.php");
$getsite = $_GET['site'];
// this gets the site url to be used for the redirect -- variable from redir.php?site=$url
// do not use http://www. or a trailing slash use only the domain name root (e.g., bbc.co.uk)
$queryAA = "SELECT COUNT(*) FROM `db_table` WHERE `url` LIKE \"$getsite\"";
$dbtotalAA = mysql_query($queryAA) or die("Select Failed!");
$dbtotalAA = mysql_fetch_array($dbtotalAA);
$getIt = $dbtotalAA[0];
if($getIt < 1){
header("HTTP/1.1 302 Moved Temporarily");
header("Location: http://domain_name.com/denied.php");
}
else
{
$sql = "SELECT `ID`, `url` FROM `table_name` WHERE `url` LIKE \"$getsite\" LIMIT 1";
$sql_result = mysql_query($sql , $connection) or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($sql_result)){
$ID = $row["ID"];
$url = $row["url"];
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.$url/");
}
exit;
}
?>
hope this helps. :)