Name: Anonymous 2010-06-01 2:49
I had no idea I had a course in PHP this semester, evidently I do and the exam is tomorrow. I need to learn PHP in 18 hours and complete an assignment before the test. Teach me PHP!
CREATE TABLE users(
id int AUTO_INCREMENT,
username varchar(255),
password varchar(255)
);
<?php
session_start(); // Populate the $_SESSION-variable. Involves setting HTTP-headers, so it must be before any output.
$db = new mysqli();
if (isset($_POST['username']) && isset($_POST['password'])) {
$user = $_POST['username'];
$pass = sha1($_POST['password']);
$res = $db->query("SELECT id FROM users WHERE username = '".$db->real_escape_string($user)."' AND password = '".$pass."';");
if ($res->num_rows >= 1) {
$_SESSION['username'] = $user;
list($_SESSION['userid']) = $res->fetch_row();
} else {
fail_hard();
}
} elseif (isset($_POST['regusername'])) {
// &c., &c.
$pass = sha1($_POST['pass']);
$db->query("INSERT INTO users (username, password) VALUES ('".$db->real_escape_string($username)."', '".$pass."');");
}