php Login Logut with session PHP Share on Facebook Share on Twitter Share on Google Plus 2:56 AM Unknown 0 Comments Login Logut with session PHP login.php <?phpif($_POST){ $u=$_POST["username"]; $p=$_POST["password"];$conn = mysql_connect("localhost","root","");mysql_select_db("testdb");$result = mysql_query("SELECT * FROM form WHERE name='" . $u . "' and password = '".$p ."'");$row = mysql_fetch_array($result);if($row) { session_start();$_SESSION["user_id"] = $row['id'];$_SESSION["user_name"] = $row['name'];} else { $_SESSION["user_id"] =''; $_SESSION["user_name"] ='';$message = "Invalid Username or Password!";}}?><table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form method="post" action=""><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td colspan="3"><strong>Member Login </strong></td></tr><div class="message"><?php if($_POST){if($message!="") { echo $message; }else{if(isset($_SESSION)){header('Location:dashboard.php');}}} ?></div><tr><td width="78">Username</td><td width="6">:</td><td width="294"><input name="username" type="text" id="username"></td></tr><tr><td>Password</td><td>:</td><td><input name="password" type="password" id="password"></td></tr><tr><td> </td><td> </td><td><input type="submit" name="Submit" value="Login"><input type="reset" value="clear"><input type="button" name="Register" value="Register" onClick="location.href='register.html'"></td></tr></table></td></form></tr></table> logout.php session_start();unset($_SESSION["user_id"]);unset($_SESSION["user_name"]);echo $msg;header("Location:login.php");$msg='successfully logout!!'?> dashboard.php <?phpsession_start();?><html><head><title>User Login</title><link rel="stylesheet" type="text/css" href="styles.css" /></head><body><strong><center><tr class="tableheader"><td >User Dashboard</td><br><br></tr><tr class="tablerow"><td><?phpif($_SESSION["user_name"]) {?>Welcome <?php echo $_SESSION["user_name"]; ?>. Click here to <a href="logout.php" tite="Logout" align="right">Logout.<?php}?></td></tr></center></strong></body></html> Database create a table in the name of form and the attributes are id,name,password,email etc.. Register.php <?php$error="";if($_POST){ $password=$_POST['password']; $repassword=$_POST['repassword']; if($password==$repassword) { $name=$_POST['name']; $email=$_POST['email']; echo $email; $auth='auth'; $conn = mysql_connect("localhost","root",""); mysql_select_db("testdb"); $sql = "INSERT INTO `form`(name,email,auth,password) VALUES ('".$name."','".$email."','".$auth."','".$password."')"; $result = mysql_query($sql); if($result){ header('Location:login.php'); } } else { $error="* password doesnt match"; }}?><form method="post" action=""><center><table cellpadding="3" cellspacing="3"><tr><td><strong>Member Registeration</strong></td></tr><tr> <td>Name : <input type="text" name="name" placeholder="Enter your name" width="40px" required></td></tr><tr> <td>Email: <input type="email" name="email" width="40px" placeholder="Enter your Email" required></td></tr><tr> <td>Gender: <input type="radio" name="gender" value="male" > male <input type="radio" name="gender" value="female">female</td></tr><tr> <td>Password: <input type="password" name="password" width="40px" placeholder="Enter your Password" required></td></tr><tr> <td>re-enter Password: <input type="password" name="repassword" width="40px" placeholder="Enter your Password" required><?php if($_POST){if(isset($error)){ echo $error;}else {echo '';}} ?></td></tr><tr> <td><input type="reset" name="reset" value="reset"> <input type="submit" name="submit" value="Submit"></td></tr></table></center></form> <?php Download the Code By Unknown You Might Also Like 0 comments
0 comments