Jump to content

Help with load balancing redirect script.


RandomNsanity

Recommended Posts

Somewhat new to coding, and this is my first script so please be clear with answers :)

I am attempting to direct people to 8 different tests that I need to distribute evenly... so I'd like to redirect them to 1 of 8 links in sequential order. Details below:

 

Okay I am attempting to make a rudimentary "load balancing" script. The purpose is to redirect a user that is clicking a link, to one of eight different URLs. That should be a a very simple script with just a random math function... but I'd like to cycle through the 8 different URLs sequentially in order. I know I need to save a value in order to remember where it is when someone else clicks the link....so in comes the SQL database. I'm thinking that I can pull the value from a database, store the value as a variable in the script, then use that value as the number for the link I want (1 through 8.)

 

In order for it to loop 1 through 8 I have created a new variable and set it equal to the original database number value that was pulled, and then add 1 to that variable by incrementing. Then according to an if-else statement the new incremented value is evaluated and either set back to 1 and written back in the SQL database or just immeditely written back in the SQL database

 

- I have a problem: I've tried to test this and it keeps redirecting to the default link... I don't think I have my database settings or something right. I'm placing the script on a webserver I have hosted through iPage, and the mySQL database is on iPage also....not sure if that makes a difference because I've gotten the simple script to work. The database is complicating things. I have the database setup as 1 single table with a variable type "tinyInt". Not sure if I need to set it to enum.

It could also be that it's not setting the variable to the numerical value of the database query? Does anyone see anything blatently obvious? Any ideas on how I can edit this and get it working?

Please help, very much appreciated!

 

 

 

<?php
$con=mysqli_connect('database', 'username', 'password');
if (!$con) {
    die('Could not connect: ' . mysql_error());

mysqli_select_db($con,random_link);
$url="http://www.amazon.com";

$database_number=mysqli_query($con,"SELECT number FROM link_number");
$new_database_number=$database_number;
++$new_database_number;
if ($new_database_number="9")
  {
  $new_database_number="1";
  mysqli_query($con,"UPDATE link_number SET number = $new_database_number");
}
else
{
mysqli_query($con,"UPDATE link_number SET number = $new_database_number");
}

$link_switch=$database_number;

switch ($link_switch) {
  case "1":
    header("Location: http://www.google.com");
    break;
  case "2":
    header("Location: http://www.cnn.com");
    break;
  case "3":
    header("Location: http://www.youtube.com");
    break;
  case "4":
    header("Location: http://www.msn.com");
    break;
  case "5":
    header("Location: http://www.yahoo.com");
    break;
  case "6":
    header("Location: http://www.w3schools.com");
    break;
  case "7":
    header("Location: http://www.bing.com");
    break;
  case "8":
    header("Location: http://www.hotmail.com");
    break;
  default:
    header("Location: http://askjeeves.com");
}
?>

Edited by RandomNsanity
Link to comment
Share on other sites

First off, the terminology "load balancing" means something completely different than what you are asking for. Which may be why no one has responded yet.

 

In any event, you are making this way too complicated. Just keep a count (in the database) of when these links are used. Then you can simply use an ORDER BY in your query to get the next record.

 

This assumes the links have an ID in a specific numerical order to determine the order in which they would be displayed.

 

//Get the first link with the lowest count and lowest ID
$query = "SELECT id, link FROM links_table ORDER BY count ASC, id ASC LIMIT 1";
$result = mysql_query($query);
$link = mysql_fetch_assoc($result);
//Update the count for the selected link
$query = "UPDATE links_table SET count = count + 1 WHERE id = {$link['id']}";
$result = mysql_query($query);
//Display the link to the user
echo "<a href='{$link['link']}'>{$link['link']}</a>";
Link to comment
Share on other sites

Could you clarify a few things on this? I really like the idea its so compact and efficient but.... You have to assume I'm really inexperienced and need a bit more guidance on the most basic level.

 

 

- Could you explain a little more about connecting to the database?  Are the commands you wrote above good for calling to the database like I was?

I thought I had to use the connection I defined as $con=mysqli_connect('database', 'username', 'password');

For example every time I made a query to the database I put the $con in front, is that correct?  mysqli_query($con,"UPDATE link_number SET number = $new_database_number");

I think I can just edit the commands you have shown there and insert the connection but I'm not sure.

 

- How do I need to setup the database table for the count?

I'm thinking I need 3 columns with data about the count, id, and the URL.

 

- Are you redefining $query within this script?

I see it's used to SELECT, and then UPDATE, but from what I can see $link is defined by what $result contains... it gets changed after the UPDATE query... would that cause a problem?

 

 

As I said I'm a beginner at the coding scene so you've got to take it easy on me with the techniques you're referencing... Hell they may not even be that advanced but it's sure more than I know at this point.

 

Thank You!

Link to comment
Share on other sites

- Could you explain a little more about connecting to the database?  Are the commands you wrote above good for calling to the database like I was?

I thought I had to use the connection I defined as $con=mysqli_connect('database', 'username', 'password');

For example every time I made a query to the database I put the $con in front, is that correct?  mysqli_query($con,"UPDATE link_number SET number = $new_database_number");

I think I can just edit the commands you have shown there and insert the connection but I'm not sure.

This forum is intended for people to get help with code they have written. So there is an expectation that the person has at least a basic knowledge. I left off the DB connection in my code because I assumed you would add that (and anything else you want to include). Also, I mistakenly used the mysql_ versions and not the mysqli_

 

 

- How do I need to setup the database table for the count?

I'm thinking I need 3 columns with data about the count, id, and the URL.

That sounds correct

 

 

- Are you redefining $query within this script?

I see it's used to SELECT, and then UPDATE, but from what I can see $link is defined by what $result contains... it gets changed after the UPDATE query... would that cause a problem?

I always use the same variable to define my queries. Once I run the query, I store the result in $result. After that I no longer need the value stored in $query because I've already run it.

 

Here's a revise of what I provided previously

 

<?php
 
//Connect to DB
$con = mysqli_connect('database', 'username', 'password', 'random_link');
if (!$con) { die('Could not connect: ' . mysql_error()); } 
 
//Get the first link with the lowest count and lowest ID
$query = "SELECT id, link FROM links_table ORDER BY count ASC, id ASC LIMIT 1";
$result = mysqli_query($con, $query);
$link = mysqli_fetch_assoc($result);
//Update the count for the selected link
$query = "UPDATE links_table SET count = count + 1 WHERE id = {$link['id']}";
$result = mysqli_query($con, $query);
//Display the link to the user
echo "<a href='{$link['link']}'>{$link['link']}</a>";
 
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.