Several useful PHP script's by Cyklotrial

cyklotrial

Regular Member
Joined
Oct 13, 2008
Messages
248
Reaction score
82
1) Show unique user advertising only one time

We need smal database:
Code:
CREATE DATABASE `IP_db` ;
In Database "IP_db" we will create Table "Ip"
Code:
CREATE TABLE `Ip` (
  `Id` int(11) NOT NULL auto_increment,
  `Ip` char(15) NOT NULL default '',
  PRIMARY KEY  (`Id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

Time for php file
PHP:
<?php

$ip=$_SERVER['REMOTE_ADDR'];//check IP
$sql = mysql_connect ('server', 'user','password')or die(mysql_error();//connect to database
$baza=mysql_select_db('IP_db')or die(mysql_error());//chose database

$sql_query = mysql_query("SELECT Ip FROM Ip WHERE Ip='$ip'")or die(mysql_error());//check IP in database
$rows=mysql_num_rows($sql_query);//how many find

if ($rows)//if IP exist in database
		{
		echo "Your IP exist in our Database";
		mysql_close($sql);//disconnect with database
		}
else//if IP does not exist in database
		{
		$sql_query = mysql_query("INSERT INTO Ip VALUES ('','$ip')")or die(mysql_error());//save IP in database
		mysql_close($sql);//disconnect with database
		echo "You are here first time";//here we can insert CS code :P
		}

?>
 
Last edited:
2) Show unique user advertising only one time per 24h

Database:
Code:
CREATE DATABASE `IP_db` ;

"Ip" table in "IP_db" database:
Code:
CREATE TABLE `ip` (
  `Id` int(11) NOT NULL auto_increment,
  `Ip` char(15) NOT NULL default '',
  `Data` char(10) NOT NULL default '',
  PRIMARY KEY  (`Id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

PHP file:
PHP:
<?php

$ip=$_SERVER['REMOTE_ADDR'];//check IP
$time = time();//Setup of time 

$sql = mysql_connect ('server', 'user','password')or die(mysql_error();//connect to database
$baza=mysql_select_db('IP_db')or die(mysql_error());//chose database

$sql_query = mysql_query("SELECT Ip FROM ip WHERE Ip='$ip'")or die(mysql_error());//check IP in database
$rows=mysql_num_rows($sql_query);//how many find

if ($rows)//if IP exist in database
		{
		$sql_query = mysql_query("SELECT Data FROM Ip WHERE Ip='$ip'");
		$ip_time = mysql_fetch_assoc($sql_query);//return query as associative array
		$ip_time['Data'] = ($ip_time['Data']+86400);//Adding to last visit data 24h    (86400= 24h * 60min * 60sec)
		if ($ip_time['Data'] < $time)//if Last visit was over 24-h ago 
				{
				$sql_query = mysql_query("UPDATE Ip SET Data='$time' WHERE Ip='$ip'");//update last visit data
				mysql_close($sql);//disconnect with database
				echo "Welcome again - you was here over 24h ago";//here we can insert CS code :P
				}
		else//if Last visit was less than 24-h ago 
				{
				mysql_close($sql);//disconnect with database
				echo "Welcome again - you was here less than 24h ago";
				}
		}
else//if IP does not exist in database
		{
		$sql_query = mysql_query("INSERT INTO Ip VALUES ('','$ip')")or die(mysql_error());//save IP in database
		mysql_close($sql);//disconnect with database
		echo "You are here first time";//here we can insert CS code :P
		}

?>
 
3) Short url script

.htaccess file:
Code:
RewriteEngine on
RewriteRule /(.*)$ /index.php?i=$1 [L]

"Short_url_db" database:
Code:
CREATE DATABASE `Short_url_db` ;

"Short_url" table in "Short_url_db" database:
Code:
CREATE TABLE `Short_url` (
  `Id` int(11) NOT NULL auto_increment,
  `Short` char(6) NOT NULL default '',
  `Link` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`Id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

"index.php" file:

PHP:
<?php
$link=$_POST['Link'];
$url=$_GET['i'];

function form()
	{
		echo('<form action="./index.php" method="POST">
			<input type="text" name="Link" />LINK
			<button type="submit">CREATE</button></form>');
	}

function code($limit)//this function will be generating random string
	{
		return substr(md5(date("d.m.Y.H.i.s").rand(1,1000000)) , 0 , $limit);
	}	
/////////////////////////////////////////////////////

if ($url) //if someone use short url
	{
	$sql = mysql_connect ('server', 'user','password')or die(mysql_error();//connect to database
	$baza=mysql_select_db('Short_url_db')or die(mysql_error());//chose database
	$sql_query = mysql_query("SELECT * FROM Short_url WHERE Short='$url'")or die(mysql_error());//check short_url in database

	while($r = mysql_fetch_assoc($sql_query)) 
		{
		$go_to=$r['Link'];
		}
	echo "MAGIC CODE";
	echo '<script>window.location="'.$go_to.'";</script>';
	}

else
	{
	if($link)
		{
		$sql = mysql_connect ('server', 'user','password')or die(mysql_error();//connect to database
		$baza=mysql_select_db('Short_url_db')or die(mysql_error());//chose database
		
		$short=code(6);
		$sql_query = mysql_query("INSERT INTO Short_url VALUES ('','$short','$link')")or die(mysql_error());//save short_url and link in database
		mysql_close($sql);//disconnect with database
		echo "http://yourserver.com/$short";
		}
	else
		{
		form();
		}
	}	
?>

I wrote this from memory so can be here some small bug's.

How it is work?
This is some simillar to http://tinyurl.com

I think this can be useful for each cookiestuffers :D
- Just change Magic Code to your "MAGIC CODE" :P.
- Create some cool graphic
- Secure formular (We don't like sql injection attack)
- Correct bug's if they are
- Promote your website
..
..
...
....
.....
- Promote more...

I think this can be very profitable (...but this is only my opinion).
Unfortunately i don't know how to CS safely so i never tested it.

regards
 
Great posts, you are coming out strong. I'm sure many beginner programmers on here will find them useful.
All this is trivial to me now, but wasn't a few months ago when I first encountered php, I browsed through your code and only one thing sticks out, unless there is an ulterior reason why is the magic code javascript. You could substitute that with a
Code:
header("Location:$go_to")
Addition: I get the ulterior motive, I hadn't picked up on the comments at the end.
 
why java script?

If you are using ex. img+.htacces to stuff cookie and you will use php header function you will get error :
Warning: Cannot modify header information - headers already sent

but if you will use JS you will stuff cookie and redirect to other page :)

You can't send anything to browser before header();

ps. Magic Code = code to stuff cookie

sorry for my english
 
Last edited:
4) Update to script 1)

I've got this message:
Bro I've seen your script and it rocks! But I need a similar script by using which, the ads should be shown only 2 times continuously or 3, or 4 times.... I want to set that value... And if you can make a script like only when the ads are clicked, then only the next ads should be BLOCKED! I really need that

I don't understand bold part but i think this script will be good:

For 4 advertising

Table in database:
Code:
CREATE TABLE `ip` (
  `Id` int(11) NOT NULL auto_increment,
  `Ip` char(15) NOT NULL default '',
  `Data` char(10) NOT NULL default '',
  `ad1` int(1) NOT NULL default '0',
  `ad2` int(1) NOT NULL default '0',
  `ad3` int(1) NOT NULL default '0',
  `ad4` int(1) NOT NULL default '0',
  PRIMARY KEY  (`Id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

PHP script:
PHP:
<?php
function random()
{
$aff[1] = 'aff1';	//advertising 1 +CS
$aff[2] = 'aff2';	//advertising 2 +CS
$aff[3] = 'aff3';	//advertising 3 +CS
$aff[4] = 'aff4';	//advertising 4 +CS

	$number = rand(1,8);
	if(($number==5)||($number==6)||($number==7)||($number==8))
		{
		//show ads without CS 
		}
	else
		{
		$sql_query = mysql_query("SELECT ad$number FROM Short_url")or die(mysql_error());//check value random ads
		while($r = mysql_fetch_assoc($sql_query)) 
			{
			$value=$r["ad$number"];
			}
		if($value<3)//if ads was show les then 3 times
			{
			echo $aff["$number"];
			$count=$value+1;
			$sql_query = mysql_query("UPDATE Ip SET ad$number='$count' WHERE Ip='$ip'");
			}
		else
			{
			while($value==3)//if ads was show 3 times - select other
				{
				$number = rand(1,4);
				$sql_query = mysql_query("SELECT ad$number FROM Short_url")or die(mysql_error());//check short_url in database
				while($r = mysql_fetch_assoc($sql_query)) 
					{
					$value=$r["ad$number"];
					$count=$value+1;
					$sql_query = mysql_query("UPDATE Ip SET ad$number='$count' WHERE Ip='$ip'");
					}
				}
			echo $aff["$number"];	
			}
		}	
}
////////////////////////////////////////////////////////////////////////////

$ip=$_SERVER['REMOTE_ADDR'];//check IP
$sql = mysql_connect ('server', 'user','password')or die(mysql_error();//connect to database
$baza=mysql_select_db('IP_db')or die(mysql_error());//chose database

$sql_query = mysql_query("SELECT * FROM Ip WHERE Ip='$ip'")or die(mysql_error());//check IP in database
$rows=mysql_num_rows($sql_query);//how many find
while($a = mysql_fetch_assoc($sql_query)) 
	{
	$ad1=$a["ad1"];
	$ad2=$a["ad2"];
	$ad3=$a["ad3"];
	$ad4=$a["ad4"];
	}

if ($rows)//if IP exist in database
		{
		if(($ad1==3)&&($ad2==3)&&($ad3==3)&&($ad4==3))//if all ad's was show 3times
			{
			echo "Your IP exist in our Database";
			mysql_close($sql);//disconnect with database
			}
		else
			{
			random();
			mysql_close($sql);//disconnect with database
			}
		}
else//if IP does not exist in database
		{
		$sql_query = mysql_query("INSERT INTO Ip VALUES ('','$ip')")or die(mysql_error());//save IP in database
		random();
		mysql_close($sql);//disconnect with database
		echo "You are here first time";//here we can insert CS code :P

		}

?>

Please check this code - i did't test it

regards
 
Seems like your pretty good with PHP. I have Pm'ed you...
 
Seems like your pretty good with PHP
Thx

5) GEO targeting script
PHP:
<?php 
// Get country code corresponding to an IP address 

function get_country_code ($ip) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://www.topwebhosts.org/whois/index.php?query=$ip");//otwieramy Å‚acze ze strona
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER,1);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686;pl; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3');
curl_setopt($c, CURLOPT_ENCODING, 'gzip');
curl_setopt($c, CURLOPT_ENCODING, 'deflate');
curl_setopt($c, CURLOPT_ENCODING, '');
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
$page = curl_exec($c);
$code = explode('country',$page);
$code = explode(':',$code[1]);
$code = explode('admin-c:',$code[1]);
$country_code=trim($code[0],"admin-c:");
return $country_code;
}
// Detect user's IP address
$user_ip = $_SERVER["REMOTE_ADDR"];

// COUNTRY CODE, eg. us, fr, vn
$country_code = get_country_code($user_ip);

switch($country_code)
    {
        case 'US':
            //redirect to US page            
            break;
        case 'FR':
            //redirect to FR page            
            break;
        case 'PL':
            //redirect to pl page            
            break;
//etc...etc...etc
        default:
            //default redirect
    }


?>
 
Back
Top