ManyCodes.com – codes & scripts Get free programming codes and tutorials!

25Feb/120

What is difference between require_once(), require(), include() ?

Difference between require() and require_once():  require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING.

There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().

17Sep/103

Guess the number game in PHP

Its a game for your member in your site 😀 its very interesting 😀

use this code in your web site, if you want to entertain the visitors.


<!-- This Script is from www.hawkee.com, found at www.manycodes.com-->

<html><body>Guess a number:<br />
<form method="post" action="/guess_the_number.htm">
1. <input type="radio" name="guess" value="1"><br />
2. <input type="radio" name="guess" value="2"><br />
3. <input type="radio" name="guess" value="3"><br />
4. <input type="radio" name="guess" value="4"><br />
5. <input type="radio" name="guess" value="5"><br />
<input type="submit" name="Submit" value="Submit"></form>
</body>
</html>

<br><font face="Tahoma">
<a target="_blank" href="http://www.manycodes.com/category/php/">
<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span>
</a></font>

Filed under: PHP, PHP codes 3 Comments
17Sep/101

Ban IP using PHP

This piece of code will allow a webmaster to be able to ban a person if you know their IP.  Basically the webmaster can record the ip of the user and prohibit them to come to your website and visit your webpage.

Either create a new .php document in a text editor or add the following php to an existing .php document. (ex. index.php)

<pre><!-- This Script is from www.hawkee.com, found at www.manycodes.com-->
<?php
$ip = getenv('REMOTE_ADDR');
$blocked = "xx.xx.xx.xx";
if (ereg($blocked,$ip)) {

echo "You Have Been Banned";
exit();
}
?>
<br><font face="Tahoma"><a target="_blank" href="http://www.manycodes.com/category/php/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font>

Now to explain the code:

1. <?php - Starts the php tag. Lets the browser know what language you are using.
2. $ip = getenv('REMOTE_ADDR'); - Gets the users IP address
3. $blocked = "xx.xx.xx.xx"; - Tells the rowser that the "xx.xx.xx.xx" IP is blocked/banned
4. if (ereg($blocked,$ip)) - If the blocked/banned IP is the same as the users IP, the following echo will be displayed.
5. { - Starts a bracket

6. echo "You Have Been Banned"; - Echos the You Have Been Banned" line onto the page.
7. exit(); - Exit so no more content is ouput
8. } - Ends a bracket
9. ?> - Ends the php tag

Tagged as: , , 1 Comment
13Feb/102

How to add a simple guest book to your site

So How to add a simple guest book to your site? The code below will help visitors to your site to read your guestbook entries and post a message of their own. Very simple setup, only requires you to change 4 settings. This code uses MySQL to store the entries.

<?php
/**
* Create the table in your MySQL database:
*
* CREATE TABLE guests (
*   id int(10) NOT NULL auto_increment,
*   name varchar(50) NOT NULL,
*   message varchar(255) NOT NULL,
*   date timestamp(14) NOT NULL,
*   PRIMARY KEY (id)
* )
*
* Change the database login settings to your own
*
* The script is now ready to run
*/
// Change these to your own database settings
$host = "localhost";
$user = "username";
$pass = "password";
$db = "database";

mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
mysql_select_db($db) OR die("Could not connect to the database.");
$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y @ %H:%i') as newdate FROM guests ORDER BY id DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)) {
?>

<p><strong><?php echo $row->message; ?></strong>

<br />Posted by <?php echo $row->name; ?> on <?php echo $row->newdate; ?></p>

<?php
     }
?>

<p>Post a message</p>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p><label for="txtName">Name:</label><br />
<input type="text" title="Enter your name" name="txtName" /></p>
<p><label for="txtMessage">Your message:</label><br />
<textarea title="Enter your message" name="txtMessage"></textarea></p>
<p><label title="Send your message">
<input type="submit" value="Send" /></label></p>
</form>

<?php
}
else {
// Adds the new entry to the database
$query = "INSERT INTO guests SET message='$message', name='$name', date=NOW()";
$result = mysql_query($query);

// Takes us back to the entries
$ref = $_SERVER['HTTP_REFERER'];
header ("Location: $ref");
}

?> 

The code above is referenced from http://www.totallyphp.co.uk

Tagged as: , , , , , , 2 Comments
13Feb/100

How to make an e-mail contact form on your website?

This is a simple form mail PHP script that displays a contact form to enable visitors to your site to send the website administrator a message via email. Built in security will prevent spammers hijacking it from another domain.

<?php
/**
* Change the email address to your own.
*
* $empty_fields_message and $thankyou_message can be changed
* if you wish.
*/

// Change to your own email address
$your_email = "you@example.com";

// This is what is displayed in the email subject line

// Change it if you want
$subject = "Message via your contact form";

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

// You do not need to edit below this line

$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {
?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p><label for="txtName">Name:</label><br />
<input type="text" title="Enter your name" name="txtName" /></p>
<p><label for="txtEmail">Email:</label><br />
<input type="text" title="Enter your email address" name="txtEmail" /></p>
<p><label for="txtMessage">Your message:</label><br />
<textarea title="Enter your message" name="txtMessage"></textarea></p>
<p><label title="Send your message">
<input type="submit" value="Send" /></label></p>
</form>
<?php
}

elseif (empty($name) || empty($email) || empty($message)) {

echo $empty_fields_message;
}

else {
// Stop the form being used from an external URL
// Get the referring URL
$referer = $_SERVER['HTTP_REFERER'];

// Get the URL of this page
$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];

// If the referring URL and the URL of this page don't match then

// display a message and don't send the email.
if ($referer != $this_url) {
echo "You do not have permission to use this script from another URL.";
exit;
}

// The URLs matched so send the email
mail($your_email, $subject, $message, "From: $name <$email>");

// Display the thankyou message
echo $thankyou_message;
}
?>

The code is referenced from http://www.totallyphp.co.uk

13Feb/100

Display a different image for each day of the week

This PHP code will help you get the day of the week from the server date and then display an image (jpg or gif) to match.

<?php
/**
* Change the name of the image folder
*
* Images must be named Monday.gif, Tuesday.gif etc
*/
// Change to the location of the folder containing the images
$image_folder = "images/days";

// You do not need to edit below this line
$today = date('l');
if (file_exists($image_folder."/".$today.".gif")) {
echo "<img src=\"$image_folder/".$today.".gif\">";
}
else {
echo "No image was found for $today";
}
?>

The above code is referenced from http://www.totallyphp.co.uk

13Feb/100

Remove all characters except letters and numbers from a string

If you insert a new value to a MySQL database, and the id is an auto_increment, this simple function will obtain the id for you without having to do a new query on the database. This is the easiest way to get the id from a newly added row using a MySQL insert query without having to do a second query.

<?php
$string = "This is some text and numbers 12345 and symbols !£$%^&";
$new_string = ereg_replace("[^A-Za-z0-9]", "", $string);
echo $new_string
?>