Simple Login System
/* Login Functions */
function login($usr, $pass) {
$sql = "SELECT * FROM users WHERE usrNAME = '$usr' AND usrPASS = '$pass'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
if (mysql_num_rows($query) == 1) {
// in here is what it should do if successful
} else {
// in here is what it should do if faild
}
}
// header
<?php
if (($_POST['submit'])){
login($_POST['username'], $_POST['password']);
}
?>
// somewhere in the body section
<?php
if($_SESSION['loged'] == "false") {
echo '
<div class="loginfailed" align="center">Login Failed!</div>
';
}
?>
<form action="#" method="POST">
Username:<br />
<input name="username" type="text" tabindex="1" maxlength="45" class="inputbox" /><br />
Password:<br />
<input name="password" type="password" tabindex="2" maxlength="55" class="inputbox" /><br />
<div align="center"><input type="submit" name="submit" value="Login" tabindex="3" class="inputbutton" /></div>
</form>
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
thanks for the quick reply,
Ill edit my code.. I just i took this code form a larger project and didn't think of copying those little things...
Thanks, whould you show me how to make it ignore the '; /* part to just use the admin part...
This is also used in another project like i sed but now that you told me somethings please help me...
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
thanks, could you please expand on it i dont know what a PDO is so can you please give me code which uses mysqli... Im kinda new to php!
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Hang on i got it working but now i want to get the record id to a session variable:
eg:
<?php $_SESSION['id'] = $row['id']; ?>
Is this possible? And is session the best way to save the id for use trough out my site?
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Thanks!
From the query you gave me could you please tell me how i can add the id record to the session{'id'] thanks.
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
thanks guys this code worked!
Whould you please explain to me what a PDO is?
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
hey guys i have little problem... That code:
$_SESSION['id'] = $row[0]['id'];
did not work i think i know what the problem is; is it the $row because i cant see where its coming from...
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
thanks, this worked.. THANK YOU SO MUCH! Sorry im still learning PHP!
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Hey guys, im having so much troubles... PDO is not yet installed on my host whould there be another option and can i get code from somewhere?
I tested my site on xampp with php 5.3.3 and phpmyadmin 3.3.7 installed and it worked but my host has a bit older version.
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Hows this i customized??
Is mysql any different to myqsli?
Here is my code:
<?php
session_start();
define('ROOT', $_SERVER["SITE_HTMLROOT"].'/');
define('db_user', 'username');
define('db_pass', 'password');
define('db_name', 'database name');
function openMysql(){
mysql_connect("127.0.0.1", db_user, db_pass) or die("Could not connect to the server: " . mysql_error());
mysql_select_db(db_name);
}
openMysql();
class loginsystem {
function mss($value) {
return mysql_real_escape_string(trim(strip_tags($value)));
}
function prepare($value) {
return mysql_query($value);
}
function execute($value) {
return mysql_fetch_array($value);
}
}
function login($usr, $pass) {
$loginsystem = new loginsystem;
$username = $loginsystem->mss($usr);
$password = $loginsystem->mss($pass);
$sql = "SELECT * FROM users WHERE `username` = '".$username."' AND `password` = '".$password."' LIMIT 1";
$query = $loginsystem->prepare($sql);
$row = $loginsystem->execute($query);
if(mysql_num_rows($query) == 1 ){
$_SESSION['familyid'] = $row['Family_Code'];
$_SESSION['loggedin'] = true;
print "<meta http-equiv=\"refresh\" content=\"0;URL=".ROOT."membersarea/index.php\">";
} else {
$_SESSION['loggedin'] = false;
print "<meta http-equiv=\"refresh\" content=\"0;URL=".ROOT."index.php\">";
echo "<script type='text/javascript'>alert('Please try again!')</script>";
}
}
login($_POST['username'], $_POST['password']);
?>
Thanks,
Marais
marases
Junior Poster in Training
91 posts since Sep 2010
Reputation Points: 21
Solved Threads: 7
Skill Endorsements: 0
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0