Hello to all.
I’m trying to build a site rotator script (unique ip / site) but i can’t seem to get it figured out.
Given the fact that I’m a complete noob at php and mysql please don’t laugh too hard at my coding. Hopefully it will get better with time. I know there is a lot of room for improvement so any advice is highly valued.
The coding “seems” to be fine. the problem with it is that most of the visits will go to the first site.
PLEASE, could anyone give me a fix for this? Thank you!
Title|Hits Today|Hits Total
site 1|662|2252
site 2|117|249
site 3|109|208
site 4|122|131
DB
sites [siteid] [site_name] [visited_today] [visited_total]
usersites [idu] [ip_address] [siteid]
[PHP]<?php
function array_shuffle($array) {
if (shuffle($array)) {
return $array;
} else {
return FALSE;
}
}
$ip = $_SERVER[‘REMOTE_ADDR’];
$con=mysqli_connect(‘localhost’,’********’,’*’,'’);
$rezultat=array();
$siteuri=array();
$sql = “SELECT siteid FROM user_sites WHERE ip_address = ‘$ip’”;
$result = mysqli_query($con, $sql);
while($row = $result->fetch_array(MYSQLI_ASSOC))
{array_push($rezultat, $row[“siteid”]);}
$sql=“SELECT siteid FROM sites”;
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($result))
{array_push($siteuri, $row[“siteid”]);}
$final = array_diff($siteuri, $rezultat);
$nextvisit = $final[array_rand(array_shuffle($final))];
if ( !empty($final)){
$sql = "INSERT INTO user_sites (ip_address, siteid) VALUES ('$ip', '$nextvisit')";
$insertip = mysqli_query($con, $sql);
//print_r($nextvisit);
$sql = "SELECT url FROM sites WHERE siteid = '$nextvisit'";
$result = mysqli_query($con, $sql);
while ($row = $result->fetch_assoc()){
$url=$row['url'];
}
$sql = "UPDATE sites SET visited_today = visited_today + 1 WHERE siteid = $nextvisit";
$result = mysqli_query($con, $sql);
$sql = "UPDATE sites SET visited_total = visited_total + 1 WHERE siteid = $nextvisit";
$result = mysqli_query($con, $sql);
mysqli_close($con);
echo '<a href="'.$url.'" rel="noreferrer" id="autoclick"></a>';
echo "<script>document.getElementById('autoclick').click();</script>";
} else {
echo 'No more sites';
}
?>[/PHP]