Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function register(){
- $user = filter_input(INPUT_POST, 'user');
- $pass = filter_input(INPUT_POST, 'pass');
- $query = 'INSERT INTO usrlgin
- (user_name, user_pass)
- VALUES
- (:user, :pass)';
- $statement = $db->prepare($query);
- $statement->bindValue(':user', $user);
- $statement->bindValue(':pass', $pass);
- $statement->execute();
- $statement->closeCursor();
- }
- if (isset($_COOKIE['user_id']))
- {
- header("location:display.php");
- }
- elseif(!isset( $_POST['user'], $_POST['pass']))
- {
- $message = 'Please enter a valid username and password';
- }
- elseif(empty( $_POST['user'])||empty ( $_POST['pass']))
- {
- $message = 'Please enter a valid username and password';
- }
- else
- {
- try
- {
- require_once '../database.php';
- $user = filter_input(INPUT_POST, 'user');
- $pass = filter_input(INPUT_POST, 'pass');
- try
- {
- $query1 = 'SELECT * FROM usrlgin
- WHERE user_name=:user AND user_pass=:pass';
- $statement1 = $db->prepare($query1);
- $statement1->bindValue(':user', $user);
- $statement1->bindValue(':pass', $pass);
- $statement1->execute();
- $user_id = $statement1->fetchColumn();
- $statement1->closeCursor();
- if($user_id == false)
- {
- $message = "Login Failed";
- }
- else
- {
- $randid = rand();
- $query2 = 'SELECT cookieID FROM usrlgin';
- $statement2 = $db->prepare($query2);
- $statement2->execute();
- $checkrand = $statement2->fetchAll();
- $statement2->closeCursor();
- foreach ($checkrand as $check) {
- while($randid == $check){
- $randid = rand();
- }
- }
- $query3 = 'UPDATE usrlgin
- SET cookieID=:rand
- WHERE ID=:id';
- $statement3 = $db->prepare($query3);
- $statement3->bindValue(':rand', 'user_id'.$randid);
- $statement3->bindValue(':id', $user_id);
- $statement3->execute();
- $statement3->closeCursor();
- setcookie('user_id', 'user_id'.$randid , 0, '/');
- header("location:display.php");
- }
- }
- catch(Exception $e)
- {
- $message = 'Error';
- }
- }
- catch(Exception $e)
- {
- $message = 'Please enter Username and Password';
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Login</title>
- <link rel="stylesheet" type="text/css" href="../../css/main.css">
- </head>
- <body>
- <?php
- // put your code here
- ?>
- <header>
- <h1>Login</h1>
- </header>
- <aside class="index">
- <h1>sidebar</h1>
- <h3><a href="../../index.php">Back</a></h3>
- </aside>
- <section class="index">
- <form action="#" method="post">
- <ul>
- <li><input type="text" name="user"></li><br>
- <li><input type="text" name="pass"></li><br>
- <li><input type="submit" value="Login"></li>
- </ul>
- </form>
- <h2><?php echo $message; ?></h2>
- </section>
- <footer>
- <p>©Aaron Swede-Taillon 2016</p>
- </footer>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement