Redirect Script

Status
Not open for further replies.

bloard

New member
Dec 19, 2006
32
2
0
I see this request posted here now and then, but I want a slightly different solution than some others. If someone could post some basic php code to do this, I'd send you like $30 or so via paypal. I would like you to just post the code so that others here can use it and it will be my small contribution to the forum.

Here's what I want... I want to create a database with two tables. Table 1 has two fields... id and url. I can then redirect urls by calling the id number as a parameter and the page redirects to the url in the url field. Thus, if my script is on a page called "redirect.php", then mysite.com/redirect.php?url=324 takes me to the url from the table corresponding to id number 324.

But, I also want a separate table that records some basic stats about the click, such as referring site, date/time, url id from the other table, and user's ip address. This way I can monitor referrers for different campaigns in my content network ads for exclusion. I know this could get ever more complicated were I to ask for search strings etc., but for now this would work fine for me and would be a good starting point for all of us non programmers.

If you have further questions... ask. Thanks
 


Thanks.. i'll check it out. That link is one instance where the presell page hurts because I have no desire to read all of that garbage, but can't find the part that tells me what it does. Guess I'll come back tonight with my reading glasses and a cold beer.
 
PHP code you were looking for.

I could probably write this for you.. The only part that sucks is I will not be back home til Saturday...

I'd need a bit more benifit than $30 so if I could get some credit that would be fine w/me. Then atleast I could do some freelancing work on here with atleast a small in-use script to use as a starting portfolio...

LMK.
 
This is the begining.. I'll finish it all up sometime in the next week or so...

PHP:
<?php

/* PHP REDIRECT SCRIPT 
BY:Travis Williams (OoteR)
travis-_@_-euppc.com
*/


/// vars list
///****************************************************************************
/*
Set up all of these in order to have the script work how you want.
*/


/// sql vars
$username =    "";                    // mysql username
$password =    "";                    // mysql password
$server      =    "localhost";        // mysql server (localhost normally)

/// redirect vars
$redirect_content =    "<center>Sending you to $url</center>"; // message on redirect page
$error_message      =    "<center>Error, Link not found";        // error message
$redirect_time      =    0;                                         // time for page forward

///****************************************************************************
/// end of vars

/// connect to server
$connect = mysql_connect($server, $username, $password);

/// ack, error
if (!$connect)
{
  echo "<center>Error connecting to database!";
  die();
}

@mysql_select_db("redirect")

/// get the id of the link
$id = $_GET['ID'];

/// setup the url query
$query = "SELECT `url` FROM urls WHERE id=$id";

/// execute the query
$results = mysql_query($query);

if (mysql_num_rows($results))
{
    /// record their info into second table
    record_stats();
    /// yay, now give the url.
    $url = mysql_result($results, 0 , 'url');
 
    /// send them on their way
    echo "<meta http-equiv=\"refresh\" content=\"$redirect_time;url=$url\">";
    echo $redirect_content;
}

else 

{
    /// didnt find that id/url
    $url = $referer;
    echo "<meta http-equiv=\"refresh\" content=\"2;url=$url\">";
    echo $error_message;
}

function record_stats()
{
    /// get referrer id
    $referer = $_SERVER['HTTP_REFERER'];
    
    /// get user's ip
    $client_ip = $_SERVER['REMOTE_ADDRESS'];
    
    /// get date/time
    $date = date("d/m/y H:i");

    $record_query = "INSERT INTO stats VALUES ('','$id','$referrer','$client_ip','$date')";
    
    $insert_result = mysql_query($record_query);

}

?>


It's nothing to fancy, and untested at this point.. not to mention incomplete.. but I needed to get it online somewhere so i could copy/paste at home.
 
This should pretty much cover everything. It hasn't been tested(I wrote it in this window) so you may find some typos. But it is free after all :)

Code:
create table redirects(
id int not null auto_increment,
url varchar(255) not null,
primary key(id));

create table click_stats(
id int not null auto_increment,
clicktime datetime not null,
link int not null auto_increment,
user varchar(255) not null,
referrer varchar(255) not null,
primary key(id));

Code:
<?php
$db_user = "";
$db_pass = "";
$db_name = "";
$db_host = "";
$default_url = "http://www.casid.net";
mysql_connect($db_user,$db_pass,$db_host);
mysql_select_db($db_name);

$user = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$refer = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
if(is_int($_REQUEST['id']))
{
  mysql_query("INSERT INTO table_stats(clicktime,link,user,referrer) VALUES(NOW(),$_REQUEST['id'],'{$user}','{$refer}')");
  $res = mysql_query("SELECT url FROM redirects where id={$_REQUEST['id']}");
  if(mysql_num_rows($res))
  {
    list($url) = mysql_fetch_row($res);
    header("Location: {$url}");
    exit;
  } else {
  mysql_query("INSERT INTO table_stats(clicktime,link,user,referrer) VALUES(NOW(),-1,'{$user}','{$refer}')");
  }
} else {
  mysql_query("INSERT INTO table_stats(clicktime,link,user,referrer) VALUES(NOW(),-1,'{$user}','{$refer}')");
}
header("Location: {$default_url}");
exit;
?>
 
Status
Not open for further replies.