Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PHP

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!

Name: Anonymous 2010-06-01 10:54

>>17
For something simple I think this way is best:

Users table in MySQL:
CREATE TABLE users(
    id int AUTO_INCREMENT,
    username varchar(255),
    password varchar(255)
);


The relevant PHP-parts will then roughly be:

<?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."');");
}


>>19
Only the horrible conversion involved it in? The duck typing-part would be that a comparison with a bool casts it to a bool, I guess?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List