April 13th, 2009 | Tags:

This is a very simple and easy tutorial. Put this code were you want to add your included content / file.

<?php
include("example.php");
?>

Remember to save your page as a .php or otherwise it will not work.

April 13th, 2009 | Tags:

This is a very simple and easy tutorial. Put this code were you want to display the visiting user’s IP. <?php
echo 'User IP Address: ' .$_SERVER['REMOTE_ADDR']. '';
?>

April 13th, 2009 | Tags:

1. Begin with a blank page and put a “1” without the quotes in it and name it “counter.txt”, chmodd it to 777. 2. Create another page and put this code in it.

<?php
// File name.
$file = "counter.txt";

// Lets open a connection, shall we?
$fp = @fopen($file, "r+");

// Open the file.
$myCounter = (int)fgets($fp,10000);

// Add another entry.
$myCounter++;
rewind($fp);

// Save new entry into file.
fwrite($fp,$myCounter);

// After using something, close it.
fclose($fp);<br>

// Print results.
echo 'Page Views:' .$myCounter;
?>

3. Name this file counter.php

4. Put this code where you want the counter to be.

<?php
include("counter.php");
?>
April 13th, 2009 | Tags:

1. Begin with a blank page and save it as usersonline.txt
2. Create another page and put this code in it.

<?php

// Store user IP address.
$remote = $_SERVER["REMOTE_ADDR"];

// File name.
$file = "usersonline.txt";

//How long the user stays on, before classed offline [in seconds]
$timeoutseconds = 600;

// Current time.
$timestamp = time();
$timeout = ($timestamp-$timeoutseconds);

// Open file.
$fp = fopen("$file", "a+");
$write = $remote ."||". $timestamp;

// Save new data in file.
fwrite($fp, $write);

// Close file.
fclose($fp);

// See how much users online.
$online_array = array();
$file_array = file($file);
foreach(
$file_array as $newdata)
{
list(
$ip, $time) = explode("||", $newdata);
if(
$time >= $timeout)
{
array_push($online_array, $ip);
}
}
$online_array = array_unique($online_array);
$online = count($online_array);

echo

'Users Online:' .$online;
?>

3. Name this file usersonline.php
4. Put this code where you want the users online to show up.
<?php
include("usersonline.php");
?>

April 13th, 2009 | Tags:

This is a very simple and easy tutorial. <?php
echo 'Browser Agent:' .$_SERVER['HTTP_USER_AGENT'];
?>

April 13th, 2009 | Tags:

Display banner/buttons randomly with this short code.

<?php
// Define file names, you can add as many as you like.
$banner = array(1 => 'img1.gif', 'img2.gif', 'img3.gif');

// Lets grab a random one.
$random = rand(1, count($banner));

// Print image.
echo '<img src="' .$banner[$random]. '" border="0" alt="Random Image" />';
?>

To add more images to the set, just add them in the array.

April 13th, 2009 | Tags:

The following tutorial will teach you how to add a login/register script to your site. This tutorial requires 6 files, and PHP with MySQL installed.

Simply upload all files to any directory, open connect.php and set the connection variables. After that simply run install.php once, and when it tells you it is installed, delete install.php

You can download all the files here. Install [install.php]

<?php

require_once("connect.php");

$query = mysql_query("
CREATE TABLE `members` (
`id` MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 50 ) NOT NULL ,
`firstname` VARCHAR( 50 ) NOT NULL ,
`lastname` VARCHAR( 50 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
`date` VARCHAR( 50 ) NOT NULL ,
`ip` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
`username`
)
)"
);

echo "Installed! Please delete this file.";

?>

Connect [connect.php]

<?php

// MySQL connect information.
$c_username = "database_username";
$c_password = "database_password";
$c_host = "localhost";
$c_database = "database_name";

// Connect.
$connection = mysql_connect($c_host, $c_username, $c_password)
or die (
"It seems this site's database isn't responding.");

mysql_select_db($c_database)
or die (
"It seems this site's database isn't responding.");

?>

Register [register.php]

<?php

// Check if he wants to register:
if (!empty($_POST[username]))
undefined

?>

<html>
<head>
<title>Register</title>
</head>
<body>
<form action="register.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%">Registration</td>
</tr>
<tr>
<td width="100%"><label>Desired Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>First Name: <input type="text" name="firstname" size="25" value="<? echo $_POST[firstname]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Last Name: <input type="text" name="lastname" size="25" value="<? echo $_POST[lastname]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value="<? echo $_POST[password]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Register!"></td>
</tr>
</table>
</form>
</body>
</html>

Login [login.php]

<?php
session_start
();
// Check if he wants to login:
if (!empty($_POST[username]))
undefined

?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%">Login</td>
</tr>
<tr>
<td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>
</body>
</html>

Logout [logout.php]

<?php
$_SESSION
[username] = "";
?>

Want to limit certain files only to members, use the below script. Members [members.php]

<?php
session_start
();

// Check his status.
if (!empty($_SESSION[username])) // he got it.
undefined
else // bad info.
undefined

?>

April 13th, 2009 | Tags:

Simply add this script above your site script, and add the IP’s of the users to want to ban into the array, and you’re set!

<?php
// Define user IP.
$ip = $_SERVER['REMOTE_ADDR'];

// Allowed IP's.
$deny = array("00.000.00.00");

for (

$i = 0; $i < count($deny); $i++)
{
if (
$ip == $deny[$i])// user is not welcome.
{
// print some text + kick them out.
exit("User [IP - $ip] - <font color='red'>Access Denied!</font>");
}
}
?>

April 13th, 2009 | Tags:

For this tutorial, we’ll only need one file, lets get started.

<?php

// Each below line is another quote, add as much as you want.
$quote[] = "Quote number 0";
$quote[] = "Quote number 1";
$quote[] = "Quote number 2";

// Get random number.
$random = rand(0, count($quote) - 1);

// Lets print our the quote.
echo $quote[$random];

?>

You can see what I’m doing in the script with the comments.

Remember, arrays always start with 0, not 1.

April 13th, 2009 | Tags:

This tutorial will teach you the basics on how to use time & date with PHP.

Ever wondered how time works on the internet, very simple actually. All times are measured by the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

The measurement is called a timestamp, which contains the large date number.

You can get the timestamp of the current time with the following function:

<?php

$timestamp

= mktime();

?>

Now that we have the timestamp, we can transform it to our favorite format. For this, we’ll need to use the date() function.

Lets see an example:

<?php

$timestamp

= mktime();

// Lets show this format: day - month - year
echo date("d - m - Y", $timestamp);

?>

That was a very dull example, lets have some fun!

<?php

$timestamp

= mktime();

// Shows something like this: Today is Thursday, August 18th, 2005.
echo date("\T\o\d\a\y \i\s l, F jS, Y\.", $timestamp);

?>

As you can see above, if you want to use text within the date function, you must escape each character first with a “\”.

Thats about it, you can play with the formats even more, check out all the formats at PHP.net