...

View Full Version : simple login system



nclemale
02-22-2010, 02:57 PM
am being really cheeky here but has anyone got a simple login system using php and mysql. thats secure and safe and that i can use it for base to a new site i want to do .

i have found a few on the web and also linked thru here but they overly complicated .

its for mobile phones also so must be xhtml .

i am still learning and using an open source script at the moment. but wish to start my own and need a base to start with . thanks in advance.

JAY6390
02-22-2010, 03:53 PM
There's a few good ones here
http://www.deathmonkeyz.com/tutorials (go to the PHP section)
They're videos so it's explained how it works too

Shauny_B
02-22-2010, 03:56 PM
Whilst I'm in a good mood, here's one I've done not long ago, all the auth checks are within another class, but hey I'll show examples of how to use.

Please make sure that you have MYSQLI extention installed either on your local testing machine, and your hosting server!

I've got my database details from a class containing static methods/functions which return an instance of the MySQLi object.


<?php

/**
* LoginCheck - Checks to see if the users details (entered from login.php)
* are correct
*/
class LoginCheck
{
private $_connection;
private $_email;
private $_password;
private static $_errors = array();


public function __construct()
{
include_once("Inc/Const/Constants.php");
$this->_connection = Constants::GetDatabaseObject();
}
public function __destruct()
{
$this->_connection->close();
unset($this);
}


/**
* CheckFields - Checks to see if the email and password from the super
* global $_POST are in the correct validation format we're after ^^.
*
* @param $txtEmail - The users email which they've entered in the login
* form
* @param $txtPassword - The users password which they've entered in from the
* login form.
* @return Boolean - True if we've got the format we're after, false if
* there's errors!
*/
public function CheckFields(&$txtEmail, &$txtPassword)
{
include_once("Inc/Const/Constants.php");
if(isset($txtPassword{5}) && eregi(Constants::GetRegularExpression("email"), $txtEmail))
{
$this->_email = mysqli_real_escape_string($this->_connection, $txtEmail);
$this->_password = mysqli_real_escape_string($this->_connection,
Constants::GetReallyFunnyHash($txtPassword));
return (boolean)true;
}

self::$_errors["length"] = "Invalid email or password too short!";
return (boolean)false;
}


/**
* LookForMatchingUser - Lets browse for any matches in the database!
*
* @return Boolean - false if there's no matching data, if there
* is matching data then the user is redirected.
*/
public function LookForMatchingUser()
{
$sql = "SELECT `UserId` FROM `tbl_users`
WHERE `Email` = '$this->_email'
AND `Password` = '$this->_password';";
$query = $this->_connection->query($sql);

if($query->num_rows > 0)
{
$sqlData = $query->fetch_object();
$query->free();

$_SESSION["UserId"] = $sqlData->UserId;

header("Location: profile.php");
exit();
}

self::$_errors["Invalid"] = "Username or password is incorrect!";
return (boolean)false;
}


/**
* Errors - Any errors which have occured in the class such
* as bad validation, wrong username/password it's outputted here.
*/
public static function Errors()
{
$output = "";
foreach((array)self::$_errors as $errName => $errValue)
{
#$output .= $errName . " " . $errValue . "\n";
$output .= $errValue . "\n";
}
return (string)$output;
}


/**
* IsAlreadyLoggedIn - Checks to see if they're already logged in
* if they are why see the login form? o_O
*/
public static function IsAlreadyLoggedIn(&$session)
{
if(isset($session["UserId"]))
{
header("Location: index.php");
exit();
}
}
}

?>


Now to call/use the class just simple look at the below:



<?php
include_once("Inc/Authentication/AuthChecker.php");
include_once("Inc/Authentication/LoginCheck.php");
include_once("Inc/Authentication/LoggedInCheck.php");

LoginCheck::IsAlreadyLoggedIn($_SESSION);

if(isset($_POST["btnSubmit"]))
{
$loginCheck = &new LoginCheck();

if($loginCheck->CheckFields($_POST["txtEmail"], $_POST["txtPassword"]))
{
$loginCheck->LookForMatchingUser();
}
}
?>


Please note that there's some includes there which have different authentication classes scanning about ;)

Feel free to try/test and use.

Shaun

CallumJohnson
02-22-2010, 04:12 PM
This would be my favourite login system.
Its quality and has a nice support website too.

http://www.jp77.org/

nclemale
02-22-2010, 05:12 PM
thanks CallumJohnson i found that the other day and found it overly complicated for what i need it for. and too all the others who have replies i have found a tutorial on youtube . heres what i have so far



<?php

include("config.php");

if(isset($_POST["submit"])) {

$username=mysql_escape_string($_POST["username"]);
$password=mysql_escape_string(sha1($_POST["password"]));

if(!empty($username)&&!empty($password)){

//$sql = mysql_query("INSERT INTO users SET username='".$username."', pass='".$pass."', birthday='".$year.$month.$day."', sex='".$usex."', location='".$uloc."', email='".$email."',news='".$news."', signature='".$info."', regdate='".time()."', validated='".$validated."', ipadd='".getip()."', browserm='".browser()."'");




$sql = mysql_query("INSERT INTO users SET username='".$username."', user_password='".$password."', user_regdate='".time()."'");
echo"you are now registered";
}
}else{
echo"you must enter a username and password!";


echo'<form action="register.php" method="post">';
echo'username:<input type="text" name="username"/></br>';
echo'password:<input type="password" name="password"/></br>';
echo'<input type="submit" name="submit" value="register"/>';
echo'</form>';
}
?>


got the sessions to add still .

nclemale
02-22-2010, 06:31 PM
still having trouble . followed script to the T .

but this part dont show the username it just says
welcome,to the members only page!
where it should say welcome username to the members only page .



<?php
include("config.php");
session_start();
if ($_SESSION["loggedin"] == true){
echo"welcome".$_SESSION["username"].",to the members only page!";
}else{
echo"you are not allowed to view this page ";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum