Php

Writing To A File Using Fopen

Fadıl

Their will be many different instances when you might need to write to a file.  What we are going to do now is create an HTML form, and a PHP code so when you click submit on the HTML form it writes the users input to a file.

In this PHP script, we are going to be using the /n function, fopen, fput, and fclose commands.

First, we are going create an HTML page that has the following code.

Counting Number Of Entries In A Database

Counting Number Of Entries In A Database

Fadıl

Do you have your own database?  How would you like to know how many entries you have in your database from a PHP page?  Well its easy, I am going to show you a script that will allow you to create a counter that will count the number of entries you have in your database.

Now let’s say we have an email database, and we want to count the number of emails we have in the database.  All we need to do is create this simple script (assuming your database is already made).

How to Sending E-mail With PHP?

Fadıl

Every web site has email.  Rather it being a feedback form of some sort or a mailto: tag, why make it hard for the visitor to contact you?  When you can take 3 lines of code and create a feedback form the directly sends it to your email from the web browser with the visitor’s input.

Creating a PHP Email form could be the best thing that has ever happened to your site.  So how do you do it? Easy.
You can have any one of your pages send an email when the visitor either visits the page or if the visitor has submitted some sort of input.  When you add the code it needs to be at the top of the page you would like to send from.

Ip Check Using PHP

Fadıl

If you have ever had the need to validate an IP address then this small script is for you.

function validate_ip($ip) {
if (is_string($ip) && ereg('^([0-9]{1,3}).([0-9]{1,3}).' .
'([0-9]{1,3}).([0-9]{1,3})$',
$ip, $part)) {
if ($part[1] <= 255 && $part[2] <= 225 && $parg[3] <= 255 && $part[4] <= 255)
return TRUE; # Valid IP
}
return FALSE; # Invalid IP
}

Ok, that’s all fine and dandy but what does it mean?

The fist if the statement is breaking the IP up into 3 sections and removing the ‘.’ from the user input. It then stores the results in the $part var then the second If statement checks to see if all of the parts are valid. If everything is fine and dandy it will return TRUE and if something has gone wrong it will return FALSE.
Hope this helps anyone.

How to create a random password in PHP

Fadıl

I am trying to generate a random password in PHP.

If you ever have the need to create a random password or username you can use something like this.

function randomPassword($length) { 
  $possible = '0123456789!@#$%^&*()_+' . 
              'abcdefghijklmnopqrstuvwxyz' . 
              'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . 
  $str = ""; 
  while (strlen($str) < $length) { 
     $str .= substr($possible, (rand() % strlen($possible)), 1); 
  } 
  return($str); 
}