session - Logging Out Using PHP -
i have seen other programs regarding can't seem make work. doing wrong? want achieve after user logged out, going previous pages become restricted.
i have 3 php files:
1. sample.php - main page username , password filled in 2. successful.php - user's page after logging in 3. logout.php - destorys session hinder user access pages after logging out
sample.php
</html> <form action="" method="post"> <table> <tr> <td>username:</td> <td>password:</td> </tr> <tr> <td><input type="text" name="username"></td> <td><input type="password" name="password"></td> </tr> <tr> <td><input type="submit" value="submit" name="submit"></td> </tr> </table> </form> </html> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "login"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } if(isset($_post["submit"])){ $uname=$_post['username']; //username $pw=$_post['password']; //password //to check if fields filled in if(!$uname | !$pw){ echo "<script> window.alert('all fields required!'); window.location.href='sample.php'; </script>"; } else{ $query = "select * login_users_new username='$uname' , password='$pw'"; $result = mysqli_query($conn, $query); if(mysqli_num_rows($result) > 0) { echo "<script> window.alert('login succesfull!') window.location.href='successful.php' </script>"; } else{ echo "<script> window.alert('wrong combination!'); window.location.href='sample.php'; </script>"; } } } ?>
successful.php
<h1>congrats</h1> <h1><a href="logout.php">logout here</a> </h1> </html>
logout.php
<?php session_start(); session_destroy(); header("location: sample.php"); ?>
what should add/modify?
you should put session_start()
in every single file (so sample.php, , successful.php) want session , session persist. (you should call function before else goes browser)
Comments
Post a Comment