http://tutsforweb.blogspot.in/2012/05/registration-system-with-email.html
PHP TUTORIALS http://tutsforweb.blogspot.in/2012/09/regular-expression-in-php-part-1.html
blogger+php+tutorial+advanced
http://www.infotuts.com/easily-integrate-paypal-payment-gateway-php/#at_pco=smlwn-1.0&at_si=549aace1ef39120d&at_ab=per-14&at_pos=0&at_tot=1
In this tutorial I create a 7 file like below.
1. index.php - I write a registration form in this file.
2. configdb.php - to connect the database.
3. register.php - In this file, we will do form validation, saving user data to database and sending email to user for confirmation.
4. confirm.php - In this file, we will set the confirmation code to null if the user click the link from his email.
5. login.php - In this file, we will test whether the email and password is correct and confirmation code is null.
6. member.php - In this file, we will test whether the member is or not.
7. logout.php - In this file, we will unset the user session data.
We use the PHP $_SESSIONvariable to show the form validation error that set in the register.php.
Firstly, we test whether
the user is blank. And then whether the email is blank; if not, we also
test whether the email format is correct. If the email format is
correct, we will test whether this email has already exist. And then we will test whether the password is blank.
In this part, we will
test whether the error exit; if not, we will add the user data to our
database table and will send a mail for email verifying.
This file will active
when your click the confirmation link from his/her email. It will update
the com_code field from our database table by setting to null. Because
when we write our login page, we will also test whether the com_code field is null.
If the form
have been submitted, we will retrieve the data that is equal to the data
supplied by user and com_code must also be null. If it is true, we will
set the session variable.
If the user has already logged in, our session variable will not be blank. If it is blank we will go back to index.php or registration form.
Download Source Code
-----------------------------------------------------
To do the form validation on the server side, we need to understand about the regular expression.
Let's start with the simple example.
The above code will echo 1 because the characters "in" are found in our string.
The below code will echo 0.
The caret has another usage. I will show you in a moment.
We used the caret ^ for the meaning of the start of the string. But if you use the caret ^ within the [], it means NOT.
By the way if you use the $ withing [], it is not the end of string. It just a simple dollar sign and contains no special meaning within it.
We can use the - for the range of character class. [a-f] is equal to [abcdef].
In the above example we
use <[A-Za-z][A-Za-z0-9]*>. In this regex, < and > are
literal characters. The first character class matches a letter. The
second character class matches a letter or digit. The star repeats the
second character class. Because we used the star, it's OK if the second
character class matches nothing. So our regex will match a tag like
<B>. When matching
<HTML>, the first character class will match H. The star will
cause the second character class to be repeated three times, matching T, M and L with each step.
a{3,} 3 or more of a
a{,3} Up to 3 of a
a{3,6} 3 to 6 of a
If you want to use these eleven metacharacters ^+*.?$()|[ as literal characters in your regex, we need to escape them with a backslash.
Now let's try the real world example. Below example is to test for the email validation. It is not a prefect one, but it will validate most common email address formats correctly.
Crate a blank document in your favourite editor and paste following code in your document. And then save as mail_regex.php in your www (in wamp) folder.
Our regex pattern for the email address is like below:
As you know, email address are always in a particular format:
username @ domain . extension
For our username part we use
^[a-zA-Z0-9._%-]+
^ means our username must start with this character class [a-zA-Z0-9._%-].
+ follow by the character class so you must enter one or more of the previous character class.
For our domain part we use
[a-zA-Z0-9.-]+
Our domain name must present one or more of that character class.
And then we need to escape with the backslash to use the . as the literal character.
For the extension part we use
[a-zA-Z]{2,4}$
So your extension must present 2 to 4 of the previous character class.
Now you can create some of the regex patterns by yourself I think.
-----------------------------------------------------------------------------------
PHP TUTORIALS http://tutsforweb.blogspot.in/2012/09/regular-expression-in-php-part-1.html
blogger+php+tutorial+advanced
http://www.infotuts.com/easily-integrate-paypal-payment-gateway-php/#at_pco=smlwn-1.0&at_si=549aace1ef39120d&at_ab=per-14&at_pos=0&at_tot=1
THIS IS BEST TUTS
In this tutorial I create a 7 file like below.
1. index.php - I write a registration form in this file.
2. configdb.php - to connect the database.
3. register.php - In this file, we will do form validation, saving user data to database and sending email to user for confirmation.
4. confirm.php - In this file, we will set the confirmation code to null if the user click the link from his email.
5. login.php - In this file, we will test whether the email and password is correct and confirmation code is null.
6. member.php - In this file, we will test whether the member is or not.
7. logout.php - In this file, we will unset the user session data.
Creating database table
We need to create a user table before writing our script. Import following SQL statement via phpMyAdmin or any other MySQL tool.CREATE TABLE `user` ( `id` INT( 50 ) NOT NULL AUTO_INCREMENT , `username` VARCHAR( 50 ) NOT NULL , `email` VARCHAR( 100 ) NOT NULL , `password` VARCHAR( 20 ) NOT NULL , `com_code` VARCHAR( 255 ) default NULL, PRIMARY KEY ( `id` ) ) ENGINE = InnoDB
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sing Up</title> <style> label{ width:100px; float:left; } </style> </head> <body> <?php session_start(); if(isset($_SESSION['error'])) { echo '<p>'.$_SESSION['error']['username'].'</p>'; echo '<p>'.$_SESSION['error']['email'].'</p>'; echo '<p>'.$_SESSION['error']['password'].'</p>'; unset($_SESSION['error']); } ?> <div class="signup_form"> <form action="register.php" method="post" > <p> <label for="username">User Name:</label> <input name="username" type="text" id="username" size="30"/> </p> <p> <label for="email">E-mail:</label> <input name="email" type="text" id="email" size="30"/> </p> <p> <label for="password">Password:</label> <input name="password" type="password" id="password" size="30 "/> </p> <p> <input name="submit" type="submit" value="Submit"/> </p> </form> </div> <p><a href="login.php">Login</a></p> </body> </html>
configdb.php
<?php $mysqli=mysqli_connect('localhost','dbusername','dbpassword','databasename') or die("Database Error"); ?>
register.php
I divide this file into two parts to be clear when we discuss. In the first part, you will see the form validation.<?php session_start(); include('configdb.php'); if(isset($_POST['submit'])) { //whether the username is blank if($_POST['username'] == '') { $_SESSION['error']['username'] = "User Name is required."; } //whether the email is blank if($_POST['email'] == '') { $_SESSION['error']['email'] = "E-mail is required."; } else { //whether the email format is correct if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email'])) { //if it has the correct format whether the email has already exist $email= $_POST['email']; $sql1 = "SELECT * FROM user WHERE email = '$email'"; $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error()); if (mysqli_num_rows($result1) > 0) { $_SESSION['error']['email'] = "This Email is already used."; } } else { //this error will set if the email format is not correct $_SESSION['error']['email'] = "Your email is not valid."; } } //whether the password is blank if($_POST['password'] == '') { $_SESSION['error']['password'] = "Password is required."; }
//if the error exist, we will go to registration form if(isset($_SESSION['error'])) { header("Location: index.php"); exit; } else { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $com_code = md5(uniqid(rand())); $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error()); if($result2) { $to = $email; $subject = "Confirmation from TutsforWeb to $username"; $header = "TutsforWeb: Confirmation from TutsforWeb"; $message = "Please click the link below to verify and activate your account. rn"; $message .= "http://www.yourname.com/confirm.php?passkey=$com_code"; $sentmail = mail($to,$subject,$message,$header); if($sentmail) { echo "Your Confirmation link Has Been Sent To Your Email Address."; } else { echo "Cannot send Confirmation link to your e-mail address"; } } } } ?>
confirm.php
<?php include('configdb.php'); $passkey = $_GET['passkey']; $sql = "UPDATE user SET com_code=NULL WHERE com_code='$passkey'"; $result = mysqli_query($mysqli,$sql) or die(mysqli_error()); if($result) { echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>'; } else { echo "Some error occur."; } ?>
login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> <style> label{ width:100px; float:left; } </style> </head> <body> <?php session_start(); include('configdb.php'); if(isset($_POST['submit'])) { $email = trim($_POST['email']); $password = trim($_POST['password']); $query = "SELECT * FROM user WHERE email='$email' AND password='$password' AND com_code IS NULL"; $result = mysqli_query($mysqli,$query)or die(mysqli_error()); $num_row = mysqli_num_rows($result); $row=mysqli_fetch_array($result); if( $num_row ==1 ) { $_SESSION['user_name']=$row['username']; header("Location: member.php"); exit; } else { echo 'false'; } } ?> <div class="login_form"> <form action="login.php" method="post" > <p> <label for="email">E-mail:</label> <input name="email" type="text" id="email" size="30"/> </p> <p> <label for="password">Password:</label> <input name="password" type="password" id="password" size="30"/> </p> <p> <input name="submit" type="submit" value="Submit"/> </p> </form> </div> </body> </html>
member.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Member page</title> </head> <body> <?php session_start(); if($_SESSION['user_name'] == '') { header("Location: index.php"); exit; } echo "Hi ".$_SESSION['user_name']; ?> <a href="logout.php">Logout</a> </body> </html>
logout.php
<?php session_start(); unset($_SESSION['user_name']); header('Location: index.php'); ?>
-----------------------------------------------------
Regular expression in PHP (part 1)
Tuesday, September 11, 2012
We deal with at least one form on almost every project. So we need to
validate this form. You can do the validation with javascript on the
client side. But we also need to validate on the server side because
user can turn off the javascript.To do the form validation on the server side, we need to understand about the regular expression.
Let's start with the simple example.
<?php $string = "webinone"; echo preg_match("/in/", $string); ?>
The below code will echo 0.
<?php $string = "webinone"; echo preg_match("/ni/", $string); //output: 0 echo preg_match("/IN/", $string); //output: 0 ?>
Metacharacters
caret ^
Start of the string<?php $string = 'webinone'; echo preg_match("/^we/", $string); //output: 1 echo preg_match("/^eb/", $string); //output: 0 ?>
dollor $
End of the string<?php $string = 'webinone'; echo preg_match("/one$/", $string); //output: 1 echo preg_match("/noe$/", $string); //output: 0 ?>
Character Class []
If you code like [aeiou], it will return 1 when our string has one of the vowel.<?php $string = 'big'; echo preg_match("/[aeiou]/", $string); //Output: 1 $string1 = 'baig'; echo preg_match("/[aoiu]/", $string1); //Output: 1 $string2 = 'bag'; echo preg_match("/b[aoiu]g/", $string2); //Output: 1 $string3 = 'beg'; echo preg_match("/b[aoiu]g/", $string3); //Output: 0 $string4 = 'baog'; echo preg_match("/b[aoiu]g/", $string4); //Output: 0 ?>
<?php $string = 'webinone'; if(preg_match("/[^a]/",$string)) { echo 'String has no a.'; } ?>
We can use the - for the range of character class. [a-f] is equal to [abcdef].
<?php $string = 'webinone'; echo preg_match("/[a-z]/", $string); //Output: 1 $string1 = 'WebInOne'; echo preg_match("/[a-z]/", $string1); //Output: 1 $string2 = 'WEBINONE'; echo preg_match("/[a-z]/", $string2); //Output: 0 $string3 = '12345'; echo preg_match("/[a-zA-Z]/", $string3); //Output: 0 $string4 = '12345'; echo preg_match("/[^0-9]/", $string4); //Output: 0 ?>
Dot .
Any single character except new line (n).<?php $string = 'one'; echo preg_match("/./", $string); //Output: 1 $string1 = 'one'; echo preg_match("/[.]/", $string1); //Output: 0 $string2 = 'one'; echo preg_match("/o.e/", $string2); //Output: 1 $string3 = 'ons'; echo preg_match("/o.e/", $string3); //Output: 0 $string4 = 'onne'; echo preg_match("/o.e/", $string4); //Output: 0 $string5 = "ore"; echo preg_match("/o.e/", $string5); //Output: 1 $string6 = "one"; echo preg_match("/o.e/", $string6); //Output: 0 ?>
Asterix *
a* means 0 or more of a. We need to see the little complicate example to know its usage.<?php $string = "<html>"; echo preg_match("/<[A-Za-z][A-Za-z0-9]*>/", $string); //Output: 1 $string1 = "<b>"; echo preg_match("/<[A-Za-z][A-Za-z0-9]*>/", $string1); //Output: 1 $string2 = "<h3>"; echo preg_match("/<[A-Za-z][A-Za-z0-9]*>/", $string2); //Output: 1 $string3 = "<3>"; echo preg_match("/<[A-Za-z][A-Za-z0-9]*>/", $string3); //Output: 0 ?>
Plus +
a+ mean one or more of a.<?php $string = "php"; echo preg_match("/ph+p/", $string); //Output: 1 $string1 = "phhp"; echo preg_match("/ph+p/", $string1); //Output: 1 $string2 = "pp"; echo preg_match("/ph+p/", $string2); //Output: 0 $string3 = "12345"; echo preg_match("/[a-z]+/", $string3); //Output: 0 ?>
Question mark ?
a? Zero or one of a.<?php $string = "123456"; echo preg_match("/123-?456/", $string); //Output: 1 $string1 = "123-456"; echo preg_match("/123-?456/", $string1); //Output: 1 $string2 = "123--456"; echo preg_match("/123-?456/", $string2); //Output: 0 ?>
Curly braces {}
a{3} Exactly 3 of aa{3,} 3 or more of a
a{,3} Up to 3 of a
a{3,6} 3 to 6 of a
<?php $string = "google"; echo preg_match("/go{2}gle/", $string); //Output: 1 $string1 = "gooogle"; echo preg_match("/go{2}gle/", $string1); //Output: 0 $string2 = "gooogle"; echo preg_match("/go{2,}gle/", $string2); //Output: 1 $string3 = "google"; echo preg_match("/go{,2}gle/", $string3); //Output: 0 $string4 = "google"; echo preg_match("/go{2,3}gle/", $string4); //Output: 1 ?>
Subpattern ()
<?php $string = "This is PHP."; echo preg_match("/^(This)/", $string); //Output: 1 $string1 = "That is PHP."; echo preg_match("/^(This)/", $string1); //Output: 0 $string2 = "That is PHP."; echo preg_match("/^([0-9])/", $string2); //Output: 0 $string3 = "7 is lucky number."; echo preg_match("/^([0-9])/", $string3); //Output: 1 ?>
Logical Or |
<?php $string = "This is PHP."; echo preg_match("/^(This|That)/", $string); //Output: 1 $string1 = "That is PHP."; echo preg_match("/^(This|That)/", $string1); //Output: 1 ?>
Backslash /
Where we use backslash?If you want to use these eleven metacharacters ^+*.?$()|[ as literal characters in your regex, we need to escape them with a backslash.
<?php $string = 'webinone.net'; if(preg_match("/./",$string)) { echo 'String has dot.'; } $string1 = 'webinone+net'; if(preg_match("/+/",$string1)) { echo 'String has + sign.'; } ?>
Now let's try the real world example. Below example is to test for the email validation. It is not a prefect one, but it will validate most common email address formats correctly.
Crate a blank document in your favourite editor and paste following code in your document. And then save as mail_regex.php in your www (in wamp) folder.
<html> <head> <title>Mail text</title> </head> <body> <?php if(isset($_POST['submit'])) { $email = $_POST['email']; if(preg_match("/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/", $email)) echo "Valid mail"; else echo "Not Valid"; } ?> <form method="post" action="mail_regex.php"> <input type="text" name="email" id="email" size="30" /> <input name="submit" type="submit" value="Submit"/> </form> </body> </html>
Our regex pattern for the email address is like below:
^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$
username @ domain . extension
For our username part we use
^[a-zA-Z0-9._%-]+
^ means our username must start with this character class [a-zA-Z0-9._%-].
+ follow by the character class so you must enter one or more of the previous character class.
For our domain part we use
[a-zA-Z0-9.-]+
Our domain name must present one or more of that character class.
And then we need to escape with the backslash to use the . as the literal character.
For the extension part we use
[a-zA-Z]{2,4}$
So your extension must present 2 to 4 of the previous character class.
Now you can create some of the regex patterns by yourself I think.
-----------------------------------------------------------------------------------
No comments:
Post a Comment