Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // index.php
- <?php
- // Stops errors from showing
- error_reporting(0);
- session_start();
- if(isset($_POST['username'], $_POST['password'])){
- require 'core/connect.php';
- $query = dbConnect()->prepare("SELECT username, password FROM Users WHERE username=:username AND password=:password");
- $query->bindParam(':username', $_POST['username']);
- $query->bindParam(':password', md5($_POST['password']));
- $query->execute();
- if($row = $query->fetch()){
- $_SESSION['username'] = $row['username'];
- $_SESSION['password'] = $row['password'];
- header("Location: index.php");
- } else {
- echo '<p style="text-align: center; color: red;">Invalid username/password</p>';
- }
- if($row = $query->fetch()){
- $_SESSION['password'] = $row['password'];
- header("Location: index.php");
- }
- }
- if(isset($_SESSION['username'])) {
- echo '
- <div style="max-width: 90%; text-align: center; margin: 0 auto;">
- <img src="http://i.imgur.com/MBuIZ3h.png" width="75%" style="text-align: center; margin: 10px 0;">
- <p style="margin-left: 8px; font-size: 24px;">Welcome, <strong>'.$_SESSION['username'].'</strong><br></p><p style="margin-top: -20px; margin-left: 10px;"><a href="logout.php">Logout</a></p>
- </div>';
- } else {
- echo '
- <div style="max-width: 90%; text-align: center; margin: 0 auto;">
- <img src="http://i.imgur.com/MBuIZ3h.png" width="75%" style="text-align: center; margin: 10px 0;">
- <form method="POST" style="text-align: center; margin: 0 auto;">
- <input style="width: 100%; margin: 5px 0;" type="text" name="username" placeholder="Username"><br />
- <input style="width: 100%;" type="password" name="password" placeholder="Password"><br />
- <input style="margin-top: px; width: 100%;" type="Submit" value="Login"><br>
- <br>
- <a href="http://www.heartfx.org/registeraccount.php"><p style="margin-top: -10px;">Register an account</p></a>
- </form>
- </div>';
- }
- ?>
- // core/connect.php
- <?php
- function dbConnect(){
- try{
- $dbuser = '';
- $dbpw = '';
- $conn = new pdo("mysql:host=198.71.225.55:3306;dbname=heartfx_users;", $dbuser, $dbpw);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $conn;
- } catch(PDOException $e){
- echo 'ERROR', $e->getMessage();
- }
- }
- ?>
- // register.php
- <form method="POST">
- <?php
- if (strlen(':username')<6) {
- echo "Username must be atleast 6 characters";
- } else {
- echo '<button type="submit" style="width: 100%; padding: 10px 5px;">Register account</button><br><br>';
- }
- ?>
- </form>
- </div>
- <br><br>
- <?php
- session_start();
- if(isset($_POST['firstname'],$_POST['lastname'], $_POST['username'], $_POST['password'], $_POST['email'])){
- require 'core/connect.php';
- $query = dbConnect()->prepare("INSERT INTO Users (firstname, lastname, username, password, email) VALUES (:firstname, :lastname, :username, :password, :email)");
- $query->bindParam(':lastname', $_POST['lastname']);
- $query->bindParam(':firstname', $_POST['firstname']);
- $query->bindParam(':username', $_POST['username']);
- $query->bindParam(':password', md5($_POST['password']));
- $query->bindParam(':email', $_POST['email']);
- if (strlen($_POST['username'])<6) {
- echo 'Username must be at least 6 characters';
- } else {
- header("Location: index.php");
- }
- if($query->execute()){
- header("Location: registeredaccount.php");
- } else {
- echo 'ERROR';
- }
- }
- ?>
- // logout.php
- <?php
- session_start();
- session_destroy();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement