943,875 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3495
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 26th, 2008
0

very simple login script (checking 2 variables before writing cookie)

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // Connects to your Database
  3. mysql_connect("", "", "") or die(mysql_error());
  4. mysql_select_db("") or die(mysql_error());;
  5.  
  6. $uname = $_POST['uname'];
  7. $pword = $_POST['pword'];
  8. //gets username and password from uname and pword fields on previous page
  9.  
  10. $result = mysql_query("SELECT * FROM logins WHERE uname='$uname'");
  11.  
  12. if($row = mysql_fetch_array($result))
  13. {
  14. setcookie("loggedin", "$uname", time()+3600);
  15. echo "logged in as: ";
  16. echo $row['uname'];
  17. echo "<br />and cookie written.";
  18. echo "<br />";
  19. echo 'click <a href="cookie.php">here</a> to view cookie information.<br/><a href="login.php">Back to login page.</a>';
  20. }
  21. else
  22. {
  23. echo "wrong login information";
  24. }
  25. ?>

Thanks guyyys

Max
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Jun 26th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

make sure sanatize your inputs to protect against sql injection.

as for the login, just change your query to:

PHP Syntax (Toggle Plain Text)
  1. 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)
  1. //run query here
  2. if (mysql_num_rows($result) == 1) {
  3. //then log the person in
  4. }
  5. else {
  6. //they have invalid credentials
  7. }
Last edited by kkeith29; Jun 26th, 2008 at 11:50 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jun 26th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

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:
php Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT * FROM logins WHERE uname='$uname'");

with something like this:
php Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT * FROM logins WHERE uname='$uname' and pword='$pword'");
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jun 26th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

Ahh! keith is faster than me
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jun 27th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

ha xD Thanks guys i swear i tried that

oh wells thanks for the help guys. and ill def. include the protection against mysql injection

now just to find out what it actually is.....

Thanks again.
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Jun 29th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

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 )
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Spaiz is offline Offline
18 posts
since May 2007
Jun 29th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

yes, use sessions.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jun 29th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

Well, yes, sessions, but is there way to save user identification for long time? (except session in database/files/cookies)

Maybe some new clever way? I know the ordinary one's.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Spaiz is offline Offline
18 posts
since May 2007
Jun 29th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

Click to Expand / Collapse  Quote originally posted by Spaiz ...
Well, yes, sessions, but is there way to save user identification for long time? (except session in database/files/cookies)

Maybe some new clever way? I know the ordinary one's.
I don't think so..
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jun 29th, 2008
0

Re: very simple login script (checking 2 variables before writing cookie)

Click to Expand / Collapse  Quote originally posted by Spaiz ...
Well, yes, sessions, but is there way to save user identification for long time? (except session in database/files/cookies)

Maybe some new clever way? I know the ordinary one's.
Try to store the ipaddress of the user and date into a new table then have a timeline for how many days/months etc. on how the id will be saved in that ipadd by subtracting current date from the stored date login...Just my idea...
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Creation of Dynamic Menu for Web pages
Next Thread in PHP Forum Timeline: help please, displaying content problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC