@ 2024-05-08T12:21:05Z <?php //include('inc/config.php');
// Define variables and initialize with empty values
$username = $selected_role = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";
$host = 'localhost';
$username = 'mossgate_t';
$password = 'manchester123';
$dbname = 'mossgate_NGF';
// Create a database connection
$conn = mysqli_connect($host, $username, $password, $dbname);
// Check the connection
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
else
{
echo "error";
}
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$db = mysqli_connect($servername,$username,$password,$dbname);
if(!$db)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "error";
}
class DBController {
private $host = "localhost";
private $user = "mossgate_t";
private $password = "manchester123";
private $database = "mossgate_NGF";
private $conn;
function __construct() {
$this->conn = $this->connectDB();
}
function connectDB() {
$conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
return $conn;
}
function runQuery($query) {
$result = mysqli_query($this->conn,$query);
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = mysqli_query($this->conn,$query);
$rowcount = mysqli_num_rows($result);
return $rowcount;
}
}
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] === "POST")
{
$username = trim($_POST["username"]);
echo "in";
echo $username;
// Validate username
if(empty(trim($_POST["username"])))
{
echo "step1";
$username_err = "Please enter a username.";
echo "Please enter a username.";
}
if(!preg_match('/^[a-zA-Z0-9_]+$/', trim($_POST["username"])))
{
echo "3";
$username_err = "Username can only contain letters, numbers, and underscores.";
echo "Username can only contain letters, numbers, and underscores.";
}
else
{
echo "in2";
// Prepare a select statement
$sql = "SELECT username FROM NGF_voucher_LOGIN WHERE username = ?";
if($stmt = mysqli_prepare($db, $sql))
{ echo "step2b";
// 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))
{
echo "step2c";
//store result
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1)
{
echo "step2d";
$username_err = "This username is already taken.";
echo "This username is already taken.";
}
else
{
echo "step2e";
$username = trim($_POST["username"]);
}
}
else
{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
mysqli_stmt_close($stmt);
}
else
{
echo"hi";
}
// 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(empty($password_err) && ($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 NGF_voucher_LOGIN (username, password, role) VALUES (?,?,?)";
$selected_role = $_POST["RoleType"];
if($stmt = mysqli_prepare($db, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_password,$selected_role);
// 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: login.php");
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
mysqli_stmt_close($stmt);
}
}
// Close connection
mysqli_close($conn);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; }
.wrapper{ width: 360px; padding: 20px; }
</style>
</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">
<label>Username</label>
<input type="text" name="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
<span class="invalid-feedback"><?php echo $username_err; ?></span>
</div>
<br>
<div class="form-group">
<label for="Role">Role type:</label>
<select name="RoleType" id="RoleType">
<option value="Canteen">Canteen</option>
<option value="Shiftcontroller">Shiftcontroller</option>
<option value="Admin">Admin</option>
</select>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $password; ?>">
<span class="invalid-feedback"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirm_password" class="form-control <?php echo (!empty($confirm_password_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $confirm_password; ?>">
<span class="invalid-feedback"><?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-secondary ml-2" value="Reset">
</div>
<p>Already have an account? <a href="login.php">Login here</a>.</p>
</form>
</div>
</body>
</html>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.7 0.007 0.014 18.55 8.3.6 0.030 0.003 18.44 8.3.5 0.003 0.013 18.34 8.3.4 0.020 0.007 19.25 8.3.3 0.021 0.014 17.38 8.3.2 0.028 0.009 17.38 8.3.1 0.029 0.007 17.50 8.3.0 0.033 0.006 17.38 8.2.19 0.028 0.006 16.50 8.2.18 0.032 0.004 16.50 8.2.17 0.028 0.010 17.88 8.2.16 0.025 0.009 17.75 8.2.15 0.030 0.010 17.50 8.2.14 0.031 0.003 18.88 8.2.13 0.028 0.005 17.50 8.2.12 0.033 0.004 17.50 8.2.11 0.028 0.011 17.63 8.2.10 0.036 0.007 17.00 8.2.9 0.032 0.000 17.34 8.2.8 0.024 0.010 17.25 8.2.7 0.033 0.000 17.37 8.2.6 0.028 0.006 17.38 8.2.5 0.024 0.009 17.25 8.2.4 0.026 0.007 19.12 8.2.3 0.024 0.006 17.62 8.2.2 0.023 0.008 17.38 8.2.1 0.025 0.008 17.25 8.2.0 0.027 0.007 19.12 8.1.28 0.027 0.004 16.50 8.1.27 0.035 0.000 17.25 8.1.26 0.031 0.003 17.23 8.1.25 0.033 0.004 17.13 8.1.24 0.022 0.007 18.96 8.1.23 0.019 0.011 17.24 8.1.22 0.022 0.007 17.48 8.1.21 0.021 0.009 17.00 8.1.20 0.024 0.006 16.88 8.1.19 0.018 0.011 17.13 8.1.18 0.025 0.006 17.10 8.1.17 0.028 0.008 17.13 8.1.16 0.020 0.010 17.13 8.1.15 0.028 0.003 17.25 8.1.14 0.029 0.000 17.00 8.1.13 0.019 0.013 17.13 8.1.12 0.032 0.000 17.25 8.1.11 0.034 0.003 17.00 8.1.10 0.025 0.011 17.25 8.1.9 0.034 0.003 17.00 8.1.8 0.028 0.006 17.13 8.1.7 0.025 0.008 17.13 8.1.6 0.028 0.006 17.35 8.1.5 0.027 0.007 18.63 8.1.4 0.025 0.008 17.48 8.1.3 0.031 0.003 17.34 8.1.2 0.030 0.003 19.23 8.1.1 0.031 0.003 17.50 8.1.0 0.032 0.003 17.34
preferences:dark mode live preview ace vim emacs key bindings
37.47 ms | 403 KiB | 5 Q