• support@answerspoint.com

how to create a login page with PHP and MySQL.

2211

Me create a login page in PHP and Connect with MySQL Database, But my code is not successfully login, Help me to Solve it. 

1Answer


0

If you want to create a login page with PHP and MySQL at first you have to create database. if you are using mysql use phpmyadmin to create database

Config.php

Config.php file is having information about MySQL Data base configuration.

 
<?php
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'login_test';
	$con = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $con ) 
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db($dbname);
	//echo 'Connected successfully';
?>

 

Login.php

Login PHP is having information about php script and HTML to do login.

<?php
include("config.php");
session_start();
$lmsg="";
if($_POST) 
{
	  $user_id = $_POST['user_id'];
	  $user_pass = $_POST['user_pass']; 
	  
	  $sql = "SELECT * FROM login WHERE user_id = '$user_id'";
	  $result = mysql_query($sql) or die(mysql_error());
	  $count = mysql_num_rows($result);
	  //echo $count;
		
	  if($count == 1) 
	  {
		  $info=mysql_fetch_array($result);
		  if($info['user_pass']==$user_pass)
		  {
			  $_SESSION['rmg']= "$user_id";
			 header("location:welcome.php"); 
		  }
		  else
		  {
			  $lmsg = "password not match";  
		  }
	  }
	  else
	  {
		$lmsg= "Invalid User";  
	  }
   }
?>
<html>
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
			padding:5px;
         }
      </style>
      
   </head>
   <body>
	
      <div align = "center">
         <div style = "width:300px; border: solid 1px #000; " align = "left">
            <div style = "background-color:#ccc; color:#000; padding:10px;"><b>Login</b></div>
            <div style = "margin:30px">
            	<form action ="" method = "post">
                  <label><?php echo $lmsg;?> <br /><br />
                  <label>UserName  :</label><input type = "text" name = "user_id" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "user_pass" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>
					
            </div>
         </div>
      </div>

   </body>
</html>

 

welcome.php

welcome.php it will display After successful login.

<?php
   session_start();
?>
<html">
   <head>
      <title>Welcome page </title>
   </head>
   
   <body>
   	<h1>Welcome <?php echo $_SESSION['rmg'] ?></h1> 
   </body>
</html>

 

  • answered 6 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 6 years ago
  • viewed 2211 times
  • active 6 years ago

Best Rated Questions