Php site rotator problem

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]

Well, not sure what your shuffle code does for you. Why would you want to waste all that time in your code.
I think this is not really what you want to use. First, the shuffle() function will renumber the indexes for items in
the array. If you need that functionality for some reason, then it is the correct function to use. But, to just pull
our a random item from an array, you might want to use the array_rand() function instead. That function will
pull out one or a set number of items from the array. It seems silly to me to keep shuffling an array over and
over, when you just want to pull out a rand item from it. No shuffling needed. Instead of the function, just do
a random pull from the array.

I guess what I am thinking is that you shuffle the deck and then pull out a random card. Quite often, you will get
the same card because both the shuffle and the random pick will duplicate the randomness of the selection.
It seems better to just pull out a randon card from the deck. (Sorry, I play poker… LOL)
EDIT: I mean, a deck of four sites. Shuffle them. Pick one of four sites. Shuffle again. Pick again. Quite
often, the same site would be selected. I would have to look up the odds to figure it out…

Now, with that said, to force a better and more uniformed selection process, you could just sort the array by
the total hit count and select the one with the least number of hits. That would force all of the sites to be about
the same number of hits at all times. That process might work well for you.

Not sure if those thoughts help or not, but, something to think about… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service