Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //if the form was submit
- if(isset($_POST['submit'] && !empty($_POST['username']))){
- $username = $_POST['username'];
- $password = $_POST['password'];
- try{//connection to database(some database)
- $dbh = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",$username,$password,$options);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- //check if the username exists and the password is the same as in the database
- $sql="SELECT * FROM users WHERE username=$username AND password=$password";//match username and password
- $result = $dbh->prepare($sql);
- $result->execute();
- if($result){
- return;// (what ever we want to make the connection)
- //we can set cookie/session or what ever we wish to make the username logged.
- }
- return false;//username or password not match, no connection
- }
- catch(PDOException $e){
- throw new Exception($e->getMessage());
- }
- }
- }
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Login</title>
- </head>
- <body>
- <form action="" method="POST">
- <label>Username:</label><input type="text" name="username" /><br/>
- <label>Password:</label><input type="password" name="password" /><br/>
- <input type="submit" name="submit" value="Connect" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement