| | |
very simple login script (checking 2 variables before writing cookie)
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi all,
I have just put together a simple log in script from various tutorials on the web and at the moment it is only checking the username entered by the user against what is in my database.
I cant find any infromation about any kind of AND function or any other check the password as well as the username so thats what i need help with
here is what i have already:
Thanks guyyys
Max
I have just put together a simple log in script from various tutorials on the web and at the moment it is only checking the username entered by the user against what is in my database.
I cant find any infromation about any kind of AND function or any other check the password as well as the username so thats what i need help with
here is what i have already:
PHP Syntax (Toggle Plain Text)
<?php // Connects to your Database mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error());; $uname = $_POST['uname']; $pword = $_POST['pword']; //gets username and password from uname and pword fields on previous page $result = mysql_query("SELECT * FROM logins WHERE uname='$uname'"); if($row = mysql_fetch_array($result)) { setcookie("loggedin", "$uname", time()+3600); echo "logged in as: "; echo $row['uname']; echo "<br />and cookie written."; echo "<br />"; echo 'click <a href="cookie.php">here</a> to view cookie information.<br/><a href="login.php">Back to login page.</a>'; } else { echo "wrong login information"; } ?>
Thanks guyyys

Max
Ill solve somebody's thread someday! xD
make sure sanatize your inputs to protect against sql injection.
as for the login, just change your query to:
also, a better way to do a login script is to see the number of results returned from the query.
ex.
as for the login, just change your query to:
PHP Syntax (Toggle Plain Text)
SELECT * FROM logins WHERE uname='$uname' AND password='$pword'
also, a better way to do a login script is to see the number of results returned from the query.
ex.
PHP Syntax (Toggle Plain Text)
//run query here if (mysql_num_rows($result) == 1) { //then log the person in } else { //they have invalid credentials }
Last edited by kkeith29; Jun 26th, 2008 at 11:50 pm.
you can change your query like this to validate a username with its password.
(let us say pword is your password table in db...)
replace this:
with something like this:
(let us say pword is your password table in db...)
replace this:
php Syntax (Toggle Plain Text)
$result = mysql_query("SELECT * FROM logins WHERE uname='$uname'");
with something like this:
php Syntax (Toggle Plain Text)
$result = mysql_query("SELECT * FROM logins WHERE uname='$uname' and pword='$pword'");
•
•
Join Date: May 2007
Posts: 14
Reputation:
Solved Threads: 0
May I ask related question in here?
I was just reading around, found this thread and remembered that I always wanted to know if it's possible to query database only once, at first visit, to confirm login/pwd is correct.
Basicaly, is there a way to let user browse protected area without checking the database on every page view? (saving id in cookies is not a way
)
I was just reading around, found this thread and remembered that I always wanted to know if it's possible to query database only once, at first visit, to confirm login/pwd is correct.
Basicaly, is there a way to let user browse protected area without checking the database on every page view? (saving id in cookies is not a way
) ![]() |
Other Threads in the PHP Forum
- Previous Thread: Creation of Dynamic Menu for Web pages
- Next Thread: help please, displaying content problem
| Thread Tools | Search this Thread |
apache api array basic beginner binary body broken cakephp checkbox class cms code computing cron curl database date date/time delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert ip javascript joomla limit link list login mail memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms snippet source space sql static syntax system table thesishelp tutorial update upload url validator variable video web webdesign xml youtube






