brsy
nah mang
- May 12, 2011
- 1,530
- 272
This is a simple PHP script to login. All you need is a database connection, table called 'users' with the columns 'username', and 'password'. You also will need an HTML form. All the main PHP is coded for you already. Everything is already commented, so you can easily maneuver around and edit things as you wish.
[/PHP][/FONT]
PHP:
<?php[/FONT]
[FONT=Arial]/*****************************************[/FONT]
[FONT=Arial]* Script Name: SupaScripts: Login[/FONT]
[FONT=Arial]* Created by: _DucklingX[/FONT]
[FONT=Arial]* Created on: August 3rd, 10:03 PM EST[/FONT]
[FONT=Arial]*****************************************/[/FONT]
[FONT=Arial]function clean($string) {[/FONT]
[FONT=Arial] return mysql_real_escape_string(htmlentities($string)); # Prevents MySQL Injections #[/FONT]
[FONT=Arial]}[/FONT]
[FONT=Arial]function loginUser($username, $password) {[/FONT]
[FONT=Arial] if(isset($_POST['username'] && isset($_POST['password']) { # If both values are set #[/FONT]
[FONT=Arial] $username = clean($username); # Clean the param to prevent SQL injection #[/FONT]
[FONT=Arial] $password = clean($password); # Clean the param to prevent SQL injection # [/FONT]
[FONT=Arial] $query = mysql_query("SELECT * FROM users WHERE username = '" . $username . "'"); # Set the query #[/FONT]
[FONT=Arial] $row = mysql_fetch_array($query); # Fetch the array from the database #[/FONT]
[FONT=Arial] if($row['password'] != $password) { # If database password doesnt equal password typed in... #[/FONT]
[FONT=Arial] die("The password you have entered does not match our records."); # Return an errror #[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] elseif() { # If there are no errors... #[/FONT]
[FONT=Arial] session_start(); # Start the session #[/FONT]
[FONT=Arial] $_SESSION['username'] = $username; # Set the username value, within the $_SESSION variable #[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial][FONT=Arial] }[/FONT]
[FONT=Arial][FONT=Arial]}[/FONT]
[FONT=Arial]# Usage... #[/FONT]
[FONT=Arial]loginUser($_POST['username'], $_POST['password']);[/FONT]
[FONT=Arial]?>