|
· AniB. Newsletter
|
|
|


|
|
|


|
· DBGT NETWORK
|
|
|
•
Tutorials: PHP: Making a Guest online script using php and mysql
In this tutorial you will learn how to make a Guest online script using PHP and MySQL.
The theory is pretty simple, a visitors IP is stored in the database for 5 minutes until that person doesn't visit another page with the code on. Then we delete any IP's that haven't been updated for over 5 or so minutes.
We then count how many entries there are in that database table and then display them.
NOTE: You may need to change the $_SERVER[REMOTE_ADDR] to $REMOTE_ADDR if the script isnt registering ip's properly
First of all we will need to create a table for the visits to be stored on, save the below as
table.php then upload it to your browser and run, make sure you enter all the variables needed
in order to connect to your database.
<?
# Database Connection Variables
$server = "localhost";
$database = "database";
# In $database enter the database you will be using to run your guest online table on.
$username = "username";
$password = "password";
# Connect to the database
$connection = mysql_connect($server,$username,$password);
mysql_select_db($database,$connection);
mysql_query("CREATE TABLE visitors (
ip varchar(20) NOT NULL,
date int(10) DEFAULT '0' NOT NULL
)")
or die ("Cannot Create Table");
echo ("Table Created!");
?>
Now upload that an run, in your browser, if you get any errors it may be your database or the variables you entered.
Below is the users online script i will explain in comments in the script.
<?
# Database Connection Variables
$server = "localhost";
$table = "visitors";
# In the table install script we called the table visitors, so do the same here.
$database = "database";
# In $database enter the database you will be using to run your guest online table on.
$username = "username";
$password = "password";
# Connect to the database
$connection = mysql_connect($server,$username,$password);
mysql_select_db($database,$connection);
# Enter how long you want the visitors to be stored on the database before being deleted.
$minutes = 5;
# Convert the minutes to seconds to work with the timestamp
$seconds = $minutes*60;
# Now calculate what the time would be based upon what you entered for minutes
$past = time()-$seconds;
# Create a variable for the current timestamp
$now = time();
# Check to see if this visitor has already visited the site
# The WHERE is just like a condition you would have in an if() statement, ip is the field
# in the table we want to see if it matches the ip of the visitor.
# You can add additional statements using AND or OR just like you would in the if() funciton
if ( mysql_num_rows(mysql_query("SELECT * FROM $table WHERE ip=\"$_SERVER[REMOTE_ADDR]\"")) != 0 ) {
# If there is an entry with that IP update that row with the new/current time
mysql_query("UPDATE $table SET date=\"$now\" WHERE ip=\"$_SERVER[REMOTE_ADDR]\"") or die ("Error - Unable to update visit");
# Note the 'or die' will display whatever you enter in the brackets and not the default mysql error.
} else {
# If this visitor hasnt been to the site before we want to enter their ip and
# time of visit on the database.
mysql_query("INSERT INTO $table VALUES (\"$_SERVER[REMOTE_ADDR]\",\"$now\")") or die ("Error - Unable to insert records");
}
# Now we want to delete all the old entries off the database where they havent visited
# another page for 5 or so minutes
mysql_query("DELETE FROM $table WHERE date [% $past") or die ("Error - Unable to delete old visits");
# We are now going to get the amount of visitors by counting all entries in the table
$visitors = mysql_result(mysql_query("SELECT COUNT(*) FROM $table"),0);
# We use mysql_result() to get the amount of rows there are in the table, when used with
# SELECT COUNT(*) it will count all the rows in that table.
# the other alternative would be to use this:
# $visitors = mysql_num_rows(mysql_query("SELECT * FROM $table"));
# Both methods would produce the same result
echo ("<b>" . $visitors . "</b> guests online");
?>
Thats it, all finished! If you have any questions just email-me.
|
|
|
|
|

[
Home -
Staff Login -
FAQs -
Link Us -
Contact -
Privacy Policy -
Terms of Use -
Copyright -
Viewing -
Statistics
]
Dragon Ball, Dragon Ball Z, and Dragon Ball GT © 2002 BIRD STUDIO/ SHUEISHA, TOEI ANIMATION. Licensed by FUNimation® Productions, Ltd. All Rights Reserved.
Dragon Ball, Dragon Ball Z, and Dragon Ball GT and all logos, character names and distinctive likenesses thereof are trademarks of TOEI ANIMATION.
All information on this web site is solely for DBGTN.com unless otherwise stated. Not to be mistaken for the official site, DBGTN.com is strictly a fan site where by no infringement is intended.
Page loaded in 0.204629 seconds.
|