Post: Simple user system
07-24-2012, 07:58 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Extremely simple, just a little base, feel free to do as you wish

Register:

    
<?php
include ("database.php");
if (isset($_POST['username']) && ($_POST['password'])) {

$username = $database->escape($_POST['username']);
$password = $database->escape($_POST['password']);
$count = $database->query("SELECT * FROM users WHERE username='$username'")->fetch(1)->get('username'Winky Winky;
if (count($count) <= 0) {
$database->query("INSERT INTO users(username,password) VALUES('$username','$password'Winky Winky");
echo '<font color="green"><b>You are now a member</b>';
} else {
echo '<font color="red">Sorry, username is already in use</font>';
}
}

?>

<html>
<form action="" method="POST">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input type="submit" value="Register" />
</form>
</html>



Login:

    
<?php
include ("database.php");
if (isset($_POST['username']) && ($_POST['password'])) {

$username = $database->escape($_POST['username']);
$password = $database->escape($_POST['password']);
$cuser = $database->query("SELECT * FROM users WHERE username='$username'")->fetch(1)->get('username'Winky Winky;
if (count($cuser) <= 0) {
echo '<font color="red">User does not exist</font>';
} else {
if($cuser == $username)
{
$cpass = $database->query("SELECT * FROM users WHERE username='$username'")->fetch(1)->get('password'Winky Winky;
if($cuser == $username && $cpass == $password)
echo '<font color="green">You are now logged in';
session_start();
$_SESSION['username'] = $username;
}
}
}

?>

<html>
<form action="" method="POST">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input type="submit" value="Register" />
</form>
</html>



Database.php

    
<?php

class Database
{
var $con;
var $sel;
var $res;
var $assoc;
var $escape;
var $array;
var $row;

function connect()
{
$this->con = mysql_connect("localhost", "host", "") or die(mysql_error());
$this->sel = mysql_select_db("test") or die(mysql_error());
}

function close()
{
if (isset($this->connect)) {
mysql_close($this->connect());
unset($this->connect);
}
}

function query($sql)
{
$this->res = mysql_query($sql) or die(mysql_error());
return $this;
}

function fetch($type)
{
switch ($type) {
case 1:
$this->assoc = mysql_fetch_assoc($this->res);
return $this;
break;
case 2:
$this->array = mysql_fetch_array($this->res);
return $this;
break;
case 3:
$this->row = mysql_fetch_row($this->res);
return $this;
break;
}
}

function escape($escape)
{
$this->escape = mysql_real_escape_string($escape);
return $this->escape;
}


function get($col)
{
return(
$this->assoc[$col]

);
}

}
$database = new Database();
$database->connect();
?>


Logout:

    
<?php
session_destroy();
?>



user logged in:

    
<?php
session_start();
if(isset($_SESSION['username']))
{
echo 'Welcome to the user page';
}else
{
echo '<font color="red">You are not logged in dude</font>';
}
?>



You could also put the session in a class/function, and use it like:

    
$user->isloggedin();


Or whatever you'd like.
(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked RuffRage for this useful post:

07-24-2012, 09:39 PM #2
mysql_ extension is discouraged. MySQLi_ extension should be used...
if your using "mysql_" just because it "works" you should kill yourself now.
07-24-2012, 09:49 PM #3
Originally posted by KingDeath360 View Post
mysql_ extension is discouraged. MySQLi_ extension should be used...
if your using "mysql_" just because it "works" you should kill yourself now.


fuck improved mysql I'm going old motherfucking school Winky Winky

The following user thanked RuffRage for this useful post:

Gnosis

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo