This php script runs in a localhost host (in your own machine). I have used dreamweaver to create this script.
Below is the code for the above form.
Having this form, we can now create the php login script.

- if(isset($_POST['login'])){
- $link=mysql_connect("localhost","root") or die("Cannot Connect!");
- mysql_select_db("users",$link);
- $sql="select * from user where username='".$_POST['username']."' and password='".$_POST['password']."'";
- $result=mysql_query($sql);
- if(mysql_num_rows($result)==1){
- session_start();
- $_SESSION['username']=$_POST['username'];
- $_SESSION['log']=true;
- header('location: mypage.php');
- }
- else{
- echo "Invalid Username or Password! Try Again.";
- }
- }
- ?>
- Start/beginning tag of the php script.
On line 2,
- Checks if user clicks on the login button.
- it will going to create a connection to the database server which is MySQL that is running in the PhpMyAdmin interface. mysql_connect("server","user","password"). In this example, since we are running the script in a localhost, we will use localhost as the server and root for the user (note: localhost and root is the default value of PhpMyAdmin).
- If the connection attempt was successful, it will continue the script, otherwise it will return the die("") function and display what ever string you are going to set inside the function.
- If the connection attempt was successful, it will going to select the database using the mysql_select_db("dbName",connection) function. For this example, we use the database "users" and "$link" as the connection to MySQL.
- After selecting the specific database to use, we are now ready to make a MySQL query. On line 5, we have set $sql as a variable for our query string and on line 6 we are doing the actual MySQL query.
- (Note: In the query, you have noticed I have highlighted (red) the names of password and username. These are the names given to the textfields in our form in order to have a correct referencing once we run the MySQL Query)
- We do the checking, once a record is found based on our query, it will going to start the session by using the session_start() function. After calling this function, we are now going to use the $_Session['some_parameter'] variable to set for that certain user who logged in. In this example, we have used 'username' and 'log' to name our session variable. (Note: DO NOT be confused in the username of the session variable and the username in the textfield. In the session variable, any name can be defined.) Now, we have initialized the $_Session['username'] variable with the name of the user which is the $_POST['username']. Also we have initialized a boolean $_Session['log'] variable to true which will be discussed in the next topic. And finally, on line 10, after initializing all the $_Session variables, we will redirect the user to a certain page, in this case mypage.php.
- If the MySQL query did not match any condition in the database, it will display an error.
- It indicates the closing tag for php script.


- if(isset($_POST['login'])){
- $link=mysql_connect("localhost","root") or die("Cannot Connect!");
- mysql_select_db("users",$link);
- $sql="select * from user where username='".$_POST['username']."' and password='".$_POST['password']."'";
- $result=mysql_query($sql);
- if(mysql_num_rows($result)==1){
- session_start();
- $_SESSION['username']=$_POST['username'];
- $_SESSION['log']=true;
- header('location: mypage.php');
- }
- else{
- echo "Invalid Username or Password! Try Again.";
- }
- }
- ?>

- session_start();
- if($_SESSION['log']==true){
- echo " WELCOME ".$_SESSION['username']."! YOU HAVE LOGGED IN...";
- echo "Dont forget to Logout afterwards.";
- }
- else{
- header('location:login.php');
- }
- ?>
- Same explanation above. :-)
- It will going to check if the $_Session['log'] variable is true. This is essential for the reason that it does not allow user to directly access mypage.php webpage without logging in.
- If the $_Session['log'] variable is true, It will going to display the welcome note with the name of the user who logged in. And in line 5, it gives the user the access to logout from the website.
- If the user will going to access the mypage.php page directly without logging in, and the php script will going to check if the $_Session['log'] variable is true, it will disregard these lines, however, if $_Session['log'] variable is false, it will going to redirect in the login page.
------------------------------------ This is for the logout script --------------------------------------
If the user will going to click the logout link located in the mypage.php, it will going to run this script. On line 2, session_start() must always be in your php scripts in order to effectively use the session variables ($_Session[], session_destroy()). Now the session_destroy by its name, it will going to destroy all initialized sessions, in this case the $_SESSION['username'] and $_SESSION['log'] and on line 4, it will going to bring the user to the login.php page.
------------------------------------------------------------------------------------------------------------
There you have it, the simple login/logout php scripts.
IMPORTANT: The above scripts that does not stop hackers from using php injection. To make an advance user authentication scripts, follow this LINK. The script is basically the same, but in order to avoid hackers from using php injection and mess up with your website, you have to use encryption and mysql_real_escape_string.
Goodluck and enjoy web programming... :) Open for your comments.. thanks!

2 comments:
Use PHP mysql_real_escape_string() to prevent PHP INJECTION
Example:
$username = trim($_POST["username"]);
$password = trim(md5($_POST["password"]));
/*
use md5() function if you set your password as md5 encrypted in your database.
use trim to Strip whitespace (or other characters) from the beginning and end of a string (from PHP Manual)
*/
$sql = "SELECT * FROM users WHERE username='".mysql_real_escape_string($username).""'
AND password='".mysql_real_escape_string($password)."'
/*
now you can execute the query without worrying about php injection
*/
$result = mysql_query($sql)
^_^ regards...
The accoutrements of a gismo boutique plainly depends upon the compass and mix of jewel it has to do. There are different sizes of machines of the changeless kind pro machining aberrant sizes of castings and forgings, also there are various kinds of machines in the information of doing the exacting in any case circumspect of free in bizarre grades of refinement. An commercial considerateness is to obtain as occasional machines as reasonable to do as elegant quaint a limit of promise as tenable, and this is a- au fait not later than choosing high-grade machines which are not solely adapted to olio of in the works,
http://katalog.inforam.pl/meble/fer,marek,bienias,regaly,polkowe,w,45102.html
http://hisit.com.pl/?p=5307
http://katalog.linuxiarze.pl/tag,duch,swiety/
http://www.quarhodron.edu.pl/firmy/www,fer24,com,pl,s,4356/
http://robotallion.katowice.pl/firmy/stoly,warsztatowe,s,3332/
http://katalog.linuxiarze.pl/internet,i,komputery/platforma,aktywnych,fanow,s,79/
http://sedyr.lomza.pl/?p=3585
http://shyskel.cieszyn.pl/biznes-i-finanse/drabina-aluminiowa-zdaje-sie-byc-to-kwestia-jaka/
http://risshyash.com.pl/?p=1791
http://linkopedia.com.pl/biznes-i-ekonomia/regaly-sklepowe,k,3988.html
Post a Comment