Sunday, June 10, 2018

How To Make Signup Form .PHP

<?php

session_start();
if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session
{
header("LOCATION:welcome.php");
exit();
}
else
{
}
// fixed ... :)
?>
<br><br>
<h1 align="center"> Welcome To Signup Form </h1>

<form method="post" action="" align="center">
Name : <br><input type="text" name="name" placeholder="Enter Your Name"><br><br>
Email : <br><input type="text" name="email" placeholder="Enter Your Email"><br><br>
Password :<br> <input type="password" name="password" placeholder="Enter Your Password"><br><br>
<input type="submit" value="Signup" name="submit">

</form>

<?php

// so first we need to connect our form to the server... lets connect... :)

$con = mysqli_connect('localhost','root','','learn');

if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
//Done..

//lets make the query ... ok


$query = "INSERT INTO `users` (`name`,`email`,`password`) VALUES ('$name','$email','$password')";

$run = mysqli_query($con,$query); // here we need to pass two parameter first will, Database and second will our query.. ok

if ($run)

{
echo "<script>alert('Signup Successfull')</script>";
}
else
{
echo "<script>alert('Failed To Signup')</script>";
}
}
?>
<br>
<div align="center">
<a href="login.php"> Alredy Registed, Login</a>
</div>



How To Make Login Form PHP

<?php
//lets start the session. ok
session_start();
if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session
{
header("LOCATION:welcome.php");
exit();
}
else
{
}
// fixed
?>
<br><br>
<h1 align="center"> Welcome To Login Form </h1> 

<form method="post" action="" align="center">
Email : <br><input type="text" name="email" placeholder="Enter Your Email"><br><br>
Password :<br> <input type="password" name="password" placeholder="Enter Your Password"><br><br>
<input type="submit" value="Login" name="login"> 

</form>

<?php

$con = mysqli_connect('localhost','root','','learn'); // database connection successfull.. ok

if(isset($_POST['login']))
{
$email =  $_POST['email'];
$password = $_POST['password'];

//letst make the query

$query = "SELECT * FROM `users` WHERE `email`='$email' AND `password`='$password'";
$run = mysqli_query($con,$query);

$row = mysqli_num_rows($run);
if ($row <1 ) 
{
echo "<script>alert('Username Or Password Wrong')</script>".mysqli_error($con);
}
else
{
$data = mysqli_fetch_assoc($run);
$uname = $data['name'];
$_SESSION['uname'] = $uname;
header("LOCATION:welcome.php"); 

}

}

?>
<br>
<div align="center">

</div>

Use This Code For Welcome.php

<?php
// lets write an code, if user haven't any session then redirecting it to back login form ... ok


session_start();

if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session
{
}
else
{
header("LOCATION:login.php");
exit();
}

$name = $_SESSION['uname'];

?>
<h1 align="l">
<a href="logout.php">Logout</a>
<?php
echo "Hello," .$name. "Your Most Welcome";

// let's checking  that can we access login form after login succesful... ohh we find a bug lets fix it
?>

</h1>



Use This Code For Logout.php

<?php
session_start();;
session_destroy();
header("LOCATION:login.php");

?>

How To Make Signup Form .PHP

<?php session_start(); if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session {...