Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //try to login using cookies, but first make sure it doesn't have a $_SESSION variable for it
- if (!(isset($_SESSION['valid_user']))) {
- if (isset($_COOKIE["login"])) {
- //try to login using cookie
- $query = "select from cookie_users where uniqueid = '".$_COOKIE["login"]."')";
- $conn = new mysqli('localhost', 'user', 'password', 'test');
- $result = $conn->query($query);
- if ($result->num_rows>0) {
- $result->fetch_assoc();
- $result['username'] = $username;
- $result['password'] = $password;
- //try to login using password and username from cookie database
- $query = "select * from authorized_users where username = '".$username."' and password = '".$password."'";
- $result2 = $conn->query($query);
- if ($result->num_rows>0) {
- $_SESSION['valid_user'] = $username;
- }
- }
- }
- }
- if (isset($_POST['userid']) && isset($_POST['password'])) {
- // if the user has just tried to log in
- $userid = $_POST['userid'];
- $password = $_POST['password'];
- $db_conn=new mysqli('localhost', 'user', 'password', 'test');
- if (mysqli_connect_errno()) {
- echo 'Connection to database failed: '.mysqli_connect_errno();
- exit;
- }
- $query = 'select * from authorized_users '."where name='$userid' "."and password=sha1('$password')";
- $result = $db_conn->query($query);
- if ($result->num_rows > 0) {
- //if they are in the database, register the user id
- $_SESSION['valid_user'] = $userid;
- //set up cookie
- setcookie("login", $uniqueid, time()*60*60*24*14);
- $query = "insert into cookie_users values ('".$userid."', sha1('".$password."'), '".$uniqueid."')";
- $result = $db_conn->query($query);
- if (!$result) {
- echo 'Could not update cookie in database';
- }
- }
- $db_conn->close();
- }
- if (isset($_SESSION['valid_user'] {
- echo "members only content goes here";
- } else {
- echo "you need to login"; }
- <?php
- setcookie("test", "the test is good", time()*60*60);
- ?>
- <html>
- <body>
- <?php
- echo $_COOKIE["test"];
- ?>
- </body>
- </html>
- setcookie("login", $uniqueid, time()*60*60*24*14);
- setcookie("login", $uniqueid, time() + 60*60*24*14); // Adding two weeks, not multipying by >9000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement