Monday, June 3, 2013

Login using Php

Login using php is typical task. In login using php session maintained and direct url navigation to home page directly from url is prohibited or restricted.

Here whole code paste make long description which is restricted from youtube.So If you need code this, comment below with email Id,  will surely send you.

For full screen video of login using php visit http://www.youtube.com/watch?v=wZX6eGhF8EM&feature=youtu.be
test_login1.sql databse import file:
-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 01, 2013 at 01:42 AM
-- Server version: 5.1.36
-- PHP Version: 5.3.0

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `test_login1`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`user_id`, `username`, `password`) VALUES
(1, 'pmbtech', '317d39252445fd1a38689e1c5328e1e0'),
(2, 'test', '098f6bcd4621d373cade4e832627b4f6');




 login.php code:
<!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 :: Test</title>
<style type="text/css">
#login_div
{
    border:solid;
    border-color:#9900FF;
    position:absolute;
    margin-top:20%;
    margin-left:40%;
    padding:5px 5px 5px 5px;
}
</style>
</head>

<body>
<div id="login_div" align="center" >
    <form method="post" action="auth.php">
        Username:<input type="text" name="username" id="username" placeholder="Username" /><br/>
        Password:<input type="password" name="password" id="password" onpaste="return false" placeholder="password
        " /><br/><br/>
        <input type="submit" value="Login" />
        <input type="reset" value="Cancel" />
    </form>
</div>
</body>
</html>

auth.php code:
<?php
    session_start();
    //to connect sql server
    $con=mysql_connect("localhost","root","") or die("db server is not connected");
    // to connect database
    mysql_select_db("test_login1") or die("db selection problem");
    if(isset($_POST["username"]) && isset($_POST["password"]))
    {
        $username=$_POST["username"];
        $password=md5($_POST["password"]);
        $sql="select * from user where username='".$username."' and password='".$password."';";
        //sql select queryis executed
        $result=mysql_query($sql,$con) or die("query problem in execution");
        if(mysql_num_rows($result) > 0)
        {
            $_SESSION["username"]=$username;
            $_SESSION["is_login"]=True;
            //die("succe");
            header("Location:welcome.php");
        }
        else
        {
            die("Unauthorized activity found!!!");
        }
    }
    else
    {
        //it is for hacker who directly come this page if then redirected to login page
        //die("not set");
        header("Location:login.php");
    }   
?>

welcome.php code:
<?php
    //validation for user directly not come in welcome page through url, so redirected login page
    session_start();
    if($_SESSION["is_login"]!=True || !isset($_SESSION["is_login"]))
    {
        header("Location:login.php");
        die();
        exit();
    }
 ?>
<!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>Welcome page</title>
</head>

<body>
Welcome <?php if(isset($_SESSION["username"])) echo $_SESSION["username"];?>
</body>
</html>



No comments:

Post a Comment