Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
- Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
- Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 105
- Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
- Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
- <?php
- /* Database credentials. Assuming you are running MySQL
- server with default setting (user 'root' with no password) */
- define("DB_SERVER", "localhost");
- define("DB_USERNAME", "id2779573_root");
- define("DB_PASSWORD", "jackr0ck246");
- define("DB_DATABASE", "id2779573_dj_sql");
- // Attempt to connect to MySQL database
- $connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
- // Check connection
- if($connect === false)
- {
- die("ERROR: Could not connect. " . mysqli_connect_error());
- }
- ?>
- <!-- ----------------------------------------------------------------------- */
- /* Taken from "Tutorial Republic - PHP MySQL Login System" -
- /* ------------------------------------------------------------------------ -->
- ----PHP CODE----
- <?php
- // Include config file
- require_once 'config.php';
- // Define variables and initialize with empty values
- $username = $password = $confirm_password = "";
- $username_err = $password_err = $confirm_password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST")
- {
- // Validate username
- if(empty(trim($_POST["username"])))
- {
- $username_err = "Please enter a username.";
- }
- else
- {
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE username = ?";
- if($stmt = mysqli_prepare($connect, $sql))
- {
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_username);
- // Set parameters
- $param_username = trim($_POST["username"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt))
- {
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1)
- {
- $username_err = "This username is already taken.";
- }
- else
- {
- $username = trim($_POST["username"]);
- }
- }
- else
- {
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Validate password
- if(empty(trim($_POST['password'])))
- {
- $password_err = "Please enter a password.";
- }
- elseif(strlen(trim($_POST['password'])) < 6)
- {
- $password_err = "Password must have atleast 6 characters.";
- }
- else
- {
- $password = trim($_POST['password']);
- }
- // Validate confirm password
- if(empty(trim($_POST["confirm_password"])))
- {
- $confirm_password_err = 'Please confirm password.';
- }
- else
- {
- $confirm_password = trim($_POST['confirm_password']);
- if($password != $confirm_password)
- {
- $confirm_password_err = 'Password did not match.';
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($confirm_password_err))
- {
- // Prepare an insert statement
- $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
- if($stmt = mysqli_prepare($connect, $sql))
- {
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
- // Set parameters
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt))
- {
- // Redirect to login page
- header("location: pt-5-login.php"); // LINE 123 - ERROR OCCURRED
- }
- else
- {
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($connect);
- }
- ?>
- <!-- ----------------------------------------------------------------------------------- */
- /* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
- /* ------------------------------------------------------------------------------------ -->
- ----HTML CODE----
- <!doctype html>
- <html>
- <head>
- <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
- <meta name="description" content="Learning Portal">
- <meta name="keywords" content="Learning Portal EFREI">
- <meta name="author" content="D.Jackson">
- <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
- <link rel="icon" type="image/ico" href="favicon.ico">
- <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
- </head>
- <!-- <div> is to group block elements for CSS formatting -->
- <body>
- <div id="container">
- <header><a href="pt-5-home.htm">Register</a>
- </header>
- <!-- The htm. above which would connect to the main page was originally
- "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
- <!--
- <div id="main">
- <section>
- <p>Please enter your details:
- </p>
- -->
- <!-- ----------------------------------------------------------------------- */
- /* Resuming "Tutorial Republic - PHP MySQL Login System"
- https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
- /* ------------------------------------------------------------------------ -->
- <!--
- </head>
- <body>
- -->
- <div class="wrapper">
- <h2>Sign Up</h2>
- <p>Please fill this form to create an account.</p>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
- <div class="form-group
- <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
- <label>Username:<sup>*</sup></label>
- <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
- <span class="help-block"><?php echo $username_err; ?></span>
- </div>
- <div class="form-group
- <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
- <label>Password:<sup>*</sup></label>
- <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
- <span class="help-block"><?php echo $password_err; ?></span>
- </div>
- <div class="form-group
- <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
- <label>Confirm Password:<sup>*</sup></label>
- <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
- <span class="help-block"><?php echo $confirm_password_err; ?></span>
- </div>
- <div class="form-group">
- <input type="submit" class="btn btn-primary" value="Submit">
- <input type="reset" class="btn btn-default" value="Reset">
- </div>
- <p>Already have an account? <a href="pt-5-login.php">Login here</a>.</p>
- </form>
- </div>
- </body>
- </html>
- <!-- ----------------------------------------------------------------------- */
- /* Taken from "Tutorial Republic - PHP MySQL Login System"
- /* ------------------------------------------------------------------------ -->
- ----PHP CODE----
- <?php
- // Include config file
- require_once 'config.php';
- // Define variables and initialize with empty values
- $username = $password = "";
- $username_err = $password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST")
- {
- // Check if username is empty
- if(empty(trim($_POST["username"])))
- {
- $username_err = 'Please enter username.';
- }
- else
- {
- $username = trim($_POST["username"]);
- }
- // Check if password is empty
- if(empty(trim($_POST['password'])))
- {
- $password_err = 'Please enter your password.';
- }
- else
- {
- $password = trim($_POST['password']);
- }
- // Validate credentials
- if(empty($username_err) && empty($password_err))
- {
- // Prepare a select statement
- $sql = "SELECT username, password FROM users WHERE username = ?";
- if($stmt = mysqli_prepare($link, $sql)) // LINE 47 - ERROR OCCURRED
- }
- {
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_username);
- // Set parameters
- $param_username = $username;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt))
- {
- // Store result
- mysqli_stmt_store_result($stmt);
- // Check if username exists, if yes then verify password
- if(mysqli_stmt_num_rows($stmt) == 1)
- {
- // Bind result variables
- mysqli_stmt_bind_result($stmt, $username, $hashed_password);
- if(mysqli_stmt_fetch($stmt))
- {
- if(password_verify($password, $hashed_password))
- {
- /* Password is correct, so start a new session and
- save the username to the session */
- session_start();
- $_SESSION['username'] = $username;
- header("location: welcome.php");
- }
- else
- {
- // Display an error message if password is not valid
- $password_err = 'The password you entered was not valid.';
- }
- }
- }
- else
- {
- // Display an error message if username doesn't exist
- $username_err = 'No account found with that username.';
- }
- }
- else
- {
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt); // LINE 105 - ERROR OCCOURED
- }
- }
- // Close connection
- mysqli_close($link); // LINE 110 - ERROR OCCOURED
- }
- }
- ?>
- <!-- ----------------------------------------------------------------------------------- */
- /* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
- /* ------------------------------------------------------------------------------------ -->
- ----HTML CODE----
- <!doctype html>
- <html>
- <head>
- <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
- <meta name="description" content="Learning Portal">
- <meta name="keywords" content="Learning Portal EFREI">
- <meta name="author" content="D.Jackson">
- <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
- <link rel="icon" type="image/ico" href="favicon.ico">
- <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
- </head>
- <!-- <div> is to group block elements for CSS formatting -->
- <body>
- <div id="container">
- <header><a href="pt-5-home.htm">Login</a>
- </header>
- <!-- The htm. above which would connect to the main page was originally
- "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
- <!--
- <div id="main">
- <section>
- <p>Please enter your details:
- </p>
- -->
- <!-- ----------------------------------------------------------------------- */
- /* Resuming "Tutorial Republic - PHP MySQL Login System"
- https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
- /* ------------------------------------------------------------------------ -->
- <body>
- <div class="wrapper">
- <h2>Login</h2>
- <p>Please fill in your credentials to login.</p>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
- <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
- <label>Username:<sup>*</sup></label>
- <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
- <span class="help-block"><?php echo $username_err; ?></span>
- </div>
- <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
- <label>Password:<sup>*</sup></label>
- <input type="password" name="password" class="form-control">
- <span class="help-block"><?php echo $password_err; ?></span>
- </div>
- <div class="form-group">
- <input type="submit" class="btn btn-primary" value="Submit">
- </div>
- <p>Don't have an account? <a href="pt-5-register.php">Sign up now</a>.</p>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement