Logging Users Tutorial
Posted: Sun Jun 18, 2006 6:22 pm
In this tutorial I will be teaching you how to log information about users, save the information in a MySQL database, and then display it on webpage. First we will need to create a database. After you do that, run this query through it:
Now that we have done that, we can now start creating the pages. Name this page 'index.php' and insert this code with your database information:
That will log the users information in your database, display their information on a page, and supply a link to the other user information that we will create next. Now that we have their information logged we can display it on another page. Create a page called 'users.php' and put this code onto it:
And there you go. The code will also log the information in chronological order so the newer information is displayed first. Hope you like it, my first tutorial. Also if you would like to know when you get a new view, you can download View Notification, made by my friend Nodda4Me.
Images:


Code: Select all
CREATE TABLE `info` (
`id` int(6) NOT NULL auto_increment,
`date` varchar(30) NOT NULL default '',
`time` varchar(11) NOT NULL default '',
`ip` varchar(15) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=119 ;Code: Select all
<?
//database info here
$username="username";
$password="password";
$database="db_name";
//you can change the date and time format if you want
$date=date("l F j, Y");
$time=date("g:i:s A");
$ip=$_SERVER['REMOTE_ADDR'];
//connect to database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//insert info into database
$query = "INSERT INTO info VALUES ('','$date','$time','$ip')";
mysql_query($query);
//close connection
mysql_close();
?><h3>Your Info:</h3><br>
<b>Date:</b><?php echo date("l F j, Y"); ?><br>
<b>Time:</b><?php echo date("g:i:s A"); ?><br>
<b>IP Address:</b><?php echo $_SERVER['REMOTE_ADDR']; ?><br><br>
<a href="users.php">Other Users Info</a>Code: Select all
<?
//database info
$username="username";
$password="password";
$database="db_name";
//connect to database and get user info
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM info";
$result=mysql_query($query);
$num=mysql_numrows($result);
//close connection
mysql_close();
echo "<b><center>Visitor Information</b><br>$num Entries</center><br><br>";
$i=$num-1;
while ($i > -1) {
//get information
$date=mysql_result($result,$i,"date");
$time=mysql_result($result,$i,"time");
$id=mysql_result($result,$i,"id");
$ip=mysql_result($result,$i,"ip");
//show information
echo "<center><b>Date:</b> $date<br><b>Time:</b> $time<br><b>IP Address:</b> $ip<br><b>Visitor Number:</b> $id<hr width=25%><br></center>";
$i--;
}
?>Images:

