Jump to content

LoadBalancing by code PHP


haibec

Recommended Posts

Hi Guys!I have 2 webserver running PHP . I want make a code : With User have a number access is a odd number then that user will redirect to webserver1. If user have a number access is a even number then that user will redirect to webserver2. help me

Link to comment
Share on other sites

Hi Guys!I have 2 webserver running PHP . I want make a code : With User have a number access is a odd number then that user will redirect to webserver1. If user have a number access is a even number then that user will redirect to webserver2. help me
do you really have that many visitors that you need to run your site on two different servers? i'm not sure if this is what you want really but here it is. <?php$rand = mt_rand(1,2);if($rand == "1") { //redirect to other server}else{ //stay at this server}?>you will have to fill in the if statements with how you want to redirect the user.
Link to comment
Share on other sites

If there on the same network, it's not going to be that easy using php...The server should have some software that allows you to set a load balancer and tell it what to do if it is exceeded. The best way using php would be to have 2 addresses, like example.com and www.example.com... one of them on each server.---- ---SFB, he may have too small and old server which can't take alot to begin with, but he must have alot of people still... like 100 at any given time.

Link to comment
Share on other sites

If there on the same network, it's not going to be that easy using php...
No kidding, people don't do load balancing this way. What about the server taking all the requests to decide which server to send everyone to? There's still only one, and it would still get slammed. Also, it would totally screw up sessions if a user sent one request to one server and the next request to a different server where the 2 servers are basically independent.If you want to do load balancing or clustering, there is hardware to do exactly that.http://content.websitegear.com/article/loa...nce_methods.htmNote that writing your own script in PHP is not one of the methods :)
Link to comment
Share on other sites

do you really have that many visitors that you need to run your site on two different servers? i'm not sure if this is what you want really but here it is. <?php$rand = mt_rand(1,2);if($rand == "1") { //redirect to other server}else{ //stay at this server}?>you will have to fill in the if statements with how you want to redirect the user.
Okie Thank!But i want With User have a number access is a odd number then that user will redirect to webserver1. If user have a number access is a even number then that user will redirect to webserver2. Please help me!
Link to comment
Share on other sites

You can use % (mod operator) to see if a number is odd or even.if ($no % 2 != 0)echo "odd";elseecho "even";Probably you can also use (but it's a little harder to understand):if ($no & 1)echo "odd";elseecho "even";

Link to comment
Share on other sites

You can use % (mod operator) to see if a number is odd or even.if ($no % 2 != 0)echo "odd";elseecho "even";Probably you can also use (but it's a little harder to understand):if ($no & 1)echo "odd";elseecho "even";
Please help me detail !
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...