Thứ Tư, 21 tháng 5, 2008

How can run multi site from one script ?

Hello,
You see lots of sites that run these services as free forum service, free onlineuser, free blog hosting, free box chat, free shoutbox,... . And now, you want to run your own new service which is not popular as free shopping cart hosting, free multipixel ad hosting, free wordad hosting,.., as many as when you get a new idea for a new service.
The problem for many webmasters is "Where to get these scripts to make own service?" , for new web programmers as me is " How to switch the stand-alone script to run multi websites?" This post will give some ideas. I thinks these ideas are not new.
I use my own PHP useronline script , this is a simple script, By inserting the piece of Javascript code anywhere you want to display how many users online. You can use it this script to build a site like http://www.myonlineusers.com.

First , I design a database structure. There are 2 table, one is used to for online static, anothere is use for website list.

--
-- Table structure for table `online`
--

CREATE TABLE `online` (
`siteid` int(32) ,
`timestamp` int(15) NOT NULL default '0',
`ip` varchar(40) collate latin1_general_ci NOT NULL
) ;--
-- Table structure for table `websites`
--

CREATE TABLE `websites` (
`id` int(100) NOT NULL auto_increment,
`email` varchar(100) collate latin1_general_ci NOT NULL,
`password` varchar(100) collate latin1_general_ci NOT NULL,
`url` varchar(100) collate latin1_general_ci NOT NULL

PRIMARY KEY (`id`)
) ;


online.php :

header('Content-type: text/javascript');
require_once("config.php");
$siteid = intval($_GET['id']);
// Check the site has already registered with the service
$q = mysql_db_query($database, "SELECT * FROM `websites` WHERE `id`='".$siteid."'");
if($mysql_num_rows($q)) {
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
mysql_db_query($database, "INSERT INTO online VALUES ('".$siteid."','$timestamp','$REMOTE_ADDR')") or die("online Database INSERT Error");
mysql_db_query($database, "DELETE FROM online WHERE timestamp<$timeout") or die("online Database DELETE Error");
$result=mysql_db_query($database, "SELECT DISTINCT ip FROM online WHERE id='".$id."'") or die("online Database SELECT Error");
$user =mysql_num_rows($result);
mysql_close();
if ($user==1) {
echo " document.write('$oneperson1 $user $oneperson2') ";
}
else {echo "document.write\('$twopeople1 $user $twopeople2'\)";}
}
else echo "document.write\('Sign up your site to usersonline service'\)";
?>



We force the content type is javascript, it will be useful for user site to insert code to display current users online.

config.php;


//Database variables
$server = "localhost"; // Your MySQL Server address. This is usually localhost
$db_user = "root"; // Your MySQL Username
$db_pass = ""; // Your MySQL Password
$database = "useronline"; // Database Name

//Customizations
$timeoutseconds = "300"; // How long it it boefore the user is no longer online


//Only one person is online
$oneperson1 = "There is curently"; //Change the text that will be displayed
$oneperson2 = "Person online."; //Change the text that will be displayed

//Two or more people online
$twopeople1 ="There are currently"; //Change the text that will be displayed
$twopeople2 ="people online."; //Change the text that will be displayed

mysql_connect($server, $db_user, $db_pass) or die ("online Database CONNECT Error");
?>


That's all. We insert site id to get current online for the site which has that id.
Example: the site has site id is 2, URL will be:
http://servicesite.com/online.php?id=2


To display how many users online, user site add a piece of Javascript code:


<script type="text/javascript" src="http://servicesite.com/online.php?id=2">

</script>

Add anywhere user site want to display it.


Just a basis, the service has already ran, but we need to add some pages as register.php, login.php to let user site register and login for other purposes.

If you feel my tutorial is helpful or you have any questions,have any request about switching other scripts to multi site, feel free to post comments.