Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

29 October 2008

How to Verify Email Address Using PHP

At the previous article, we have already know how to run a PHP mail application in localhost. Now, this tutorial will show you how to verify an email address using PHP, it is in valid format or not. Please note, this article only show how to check the format, not the existence of the URL or the service provider.

To verify an email address format using PHP is easy, PHP has a great function called eregi. This is a part of PHP form validation for other entries to check proper formatted data is entered by the user.

The first step to creating a PHP script for validating an email address is work, it is good to know what characters are allowed and what characters are not allowed in an email address, states that the form of an email address must be of the form "user_account@domain". The "user_account" of an email address must be between 1 and 64 characters in length and may be made up in any one of three ways. It can be made up of a selection of characters (and only these characters) from the following selection:

  • A to Z
  • 0 to 9
  • !
  • #
  • $
  • %
  • &
  • '
  • *
  • +
  • -
  • /
  • =
  • ?
  • ^
  • _
  • `
  • {
  • }
  • ~
  • .

The "domain" of the email address can also be made up in different ways. The most common form is a domain name, which is made up of a number of "labels", each separated by a period and between 1 and 63 characters in length. Labels may contain letters, digits and hyphens, however must not begin or end with a hyphen (officially, a label must begin with a letter, not a digit, however many domain names have been registered beginning with digits so for the purposes of validation we will assume that digits are allowed at the start of domain names). A domain name, technically, need be only one label.

When you register on at a website, the site normally checks if the e-mail address that you enter is in a valid format. This is done by using what called a Regular Expression. What we need to do is check if a string matches the regular expression.

This example is using ereg function with the regular expression:


<form method=post action=<?php $_SERVER['PHP_SELF']; ?>>
Your email : <input name="email" type="text">
<input type="submit" name="check" value="Check E-Mail">
</form>

<?php>
if ($_POST[check]);
{
$valid_mail = "^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9]+)(\.[a-z]{2,3}))$"; //regular expression
if (!eregi($valid_mail,$email))
{
echo "Your email address is NOT VALID";
}
else
{
echo "Your email address is valid";
}
}
?>



Now, You can validate email addresses entered into your site.
Good Luck


References:
Lukmanul Hakim, Loko Media
http://www.addedbytes.com/
http://www.codewalkers.com/
http://www.plus2net.com/

28 October 2008

How to Run PHP Mail Application in Your Local Computer

As a web programmer, sometimes you need to test an application that correlate with email. During the time, this application must be online, because it can't run in your local computer or localhost, even you have already setup your local computer as a web server.

Fortunately, there are some software can help you to run an application that correlate with email in your local computer. One of them is ArGoSoft Mail Server (agsmail.exe), a little size freeware. You can download at http://www.argosoft.com/rootpages/download.aspx

To setup the Argosoft Mail Server, just follow these simple steps:

  • Install the Argosoft Mail Server in your local computer. If done restart your computer
  • Run the Argosoft Mail Server
  • At Tools menu, choose Option and click on it
  • Type “localhost” in the textbox, click the Add button and then click OK
  • Now, at Tools menu, choose User and click on it
  • Click the “Add New User” button
  • In User Properties window, fill the User Name, your Real Name, Password, and Confirm your Password, for example: nopomawon, Yanis Rizaldi, secret, secret.
  • Click OK if you’re done. Now, you already have an email address: nopomawon@localhost

Test it with a simple PHP application.
Now is time to test your localhost mail server. PHP has a function that can be used to send an email, it’s easy to understand and the syntax is pretty simple:

mail(recipient, subject, message, additional header)


Let’s create a simple script to test your localhost mail server. Make sure the ArGoSoft Mail Server is active.

$recepient = “nopomawon@localhost”;
$subject = “Test an email”;
$message = “Test my localhost mail server”;
$from = “replay-to:
someone@domain.com”;

$send_mail = mail($recepient, $subject, $message, $from);

If ($send_mail)
{
Echo “successfully send email”;
}
Else
{
Echo “Oops!, can’t send the email”;
}



Run the scrip. If success, you can see the mail using Microsoft Outlook or Outlook Express. Of course, you must setup the mail program as same as ArGoSoft’s User Properties.
Display Name : Yanis Rizaldi
E-mail addres : nopomawon@localhost
Account name : nopomawon
Password : secret

That’s all, just try and have some fun

Good luck

14 October 2008

How to Connect to SQL Server 2000 with PHP Using php_mssql.dll

PHP is powerful tool for build up your web applications because PHP is an easy to learn. SQL server is a Microsoft’s robust database product, which can handle terabytes of your data.

In web database application, usually some web programmers use PHP to connect to MySQL database. But in the other side, there are many web programmers looking for information how to connecting PHP to SQL Server 2000.

Well, this time I’ll share about how to connecting PHP to SQL Server using php_mssql.dll.

I use Windows Vista 32 bit for the OS, SQL Server 2000, PHP version 5.2.3 and AppServ version 2.5.9 for Windows for Web Server.

The php_mssql.dll file is already exists in extension or ext directory of your PHP installation folder. Before you can use the php_mssql.dll, you must make a modification in php.ini file, which is placed in C:\Windows directory, or If you’re using php before version 5, the php.ini file is in php installation folder.



  • Open the php.ini file using Notepad or WordPad.
  • In Notepad window, press Ctrl + F to find this word: “extension=php_mssql.dll”.
  • Delete the semicolon mark (;).
  • Save the php.ini file.
  • Restart the web server using "net stop apache" and then "net start apache" from your computer DOS promp. Or maybe you need to restart your computer.
Now, let’s test the php_mssql.dll, it is already run perfectly or not. You can check by using phpinfo() function. Type this address in your browser: http://localhost/phpinfo.php



In the PHP configuration information, you should see something like this:

Now, your Apache web server is ready to make a connection between PHP and SQL Server 2000 using php_mssql.dll.

Create connect.php file. This file is example how to connect PHP to SQL Server 2000.

$host = "rafa-comp"; //set the host with server name or ip address
$user = "sa"; //set the user id
$password =""; //set the user password (blank password is not recomended)
$database = "Northwind"; //set the database to use
if (mssql_connect($host, $user, $password)) {
echo 'Connection to SQL Server success, ';
if (mssql_select_db($database)) {
echo "$database database is selected"; }
else {
echo 'database not found';}
}
else {
echo 'Sorry my bro, connection to SQL Server failed';
}


Save the connect.php file in your web root directory. Run it through your browser. Your browser return should look like this:


OK, I hope this onformation is usefull. Have a nice day :-)