944,175 Members | Top Members by Rank

Ad:
  • PHP Code Snippet
  • Views: 5455
  • PHP RSS
0

PHP Login Script

by on Aug 22nd, 2009
Here is a simple login script
PHP Code Snippet (Toggle Plain Text)
  1. <?php
  2. $con = mysql_connect('localhost', 'root', '');
  3. $db_select = mysql_select_db('db_name', $con);
  4. if(!db_select){
  5. die("Error: No DataBase Selected.\n");
  6. }
  7. if(!$con){
  8. die("Error: ".mysql_error()."\n");
  9. }
  10. ?>
  11. <form action='./login.php' method='POST'>
  12. <table border='0' align='center'>
  13. <tr><td>Username </td><td><input type='text' name='user'></td></tr>
  14. <tr><td>Password </td><td><input type='password' name='pass'></td></tr>
  15. <tr><td colspan='2' align='right'><input type='submit' name='login' value='Login'></td></tr>
  16. </table>
  17. </form>
  18. <?php
  19. $u = $_POST['user'];
  20. $p = $_POST['pass'];
  21. $log = $_POST['login'];
  22. if($log){
  23. $sql = mysql_query("SELECT count(id) FROM `users` WHERE `username` = '$u' AND `password` = '$p'");
  24. $result = mysql_result(sql, 0);
  25. if($result!=1){
  26. die("Invalid Login Information\n");
  27. }else{
  28. echo "Welcome ".$u."! You are now logged in.\n";
  29. }
  30. }
  31. ?>
Comments on this Code Snippet
Sep 22nd, 2009
0

Re: PHP Login Script

great!! what an effort wonderful!!
Newbie Poster
vijaysankarbhat is offline Offline
1 posts
since Sep 2009
Sep 23rd, 2009
0

Re: PHP Login Script

SQL injection holes. Not secure at all. I wouldn't use it.
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Sep 29th, 2009
-1

Re: PHP Login Script

it's not useful. SQL injection.!
Newbie Poster
phong1040572 is offline Offline
1 posts
since Jun 2009
Sep 30th, 2009
0

Re: PHP Login Script

Hey! this script i'm sure is meant for beginners. If you know about SQL injection then I guess you are not a beginner and you can even do this community a favour by posting another version of the script with SQL injection holes well taken care of.
Happy times!
Junior Poster
sureronald is offline Offline
139 posts
since May 2008
Sep 30th, 2009
0

Re: PHP Login Script

nice ? but how we can add secret pages for different users
Posting Pro in Training
ayesha789 is offline Offline
485 posts
since Jun 2009
Sep 30th, 2009
0

Re: PHP Login Script

mysql_real_escape_string(); will prevent injections.

e.g.

php Syntax (Toggle Plain Text)
  1. <?php
  2. $string = 'user input';
  3. $safer = mysql_real_escape_string($string);
  4. // the variable $safer is less likely to cause you any problems from your users input.
  5. ?>

it is always best practice to hash your passwords as well {sha1($string) }. when you create the user, hash the password into the data base. when you check against it hash the password and that will give you the same result but with safer password storage.
Last edited by leviathan185; Sep 30th, 2009 at 8:35 am. Reason: forgot something
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
Sep 30th, 2009
1

Re: PHP Login Script

If you are wanting a better login script look here:

http://www.daniweb.com/forums/post95...tml#post951182
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 1st, 2009
0

Re: PHP Login Script

This script not only suffers from security holes but also has a but in recording incorrect data. If magic quotes are enabled then every recording of a slash be recorded. This means if you record the username te"s't then when you retrieve it from the database it will display te\"s\'t. To solve that you will need to use the stripslashes() function if magic quotes are enabled. Also note that the mysql_real_escape_string() function not only fixes security holes but also validates the string from potential bugs/errors. So the following is how to convert a variable ready for mysql.
php Syntax (Toggle Plain Text)
  1. <?php
  2. $data = mysql_real_escape_string(stripslashes($_POST['data']));
  3. ?>
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Oct 12th, 2009
-2

Re: PHP Login Script

please help me about php
Newbie Poster
jalaladdin is offline Offline
2 posts
since Oct 2009
Message:
Previous Thread in PHP Forum Timeline: url rewrite issue in htaccess
Next Thread in PHP Forum Timeline: PHP array Problem.





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


Follow us on Twitter


© 2011 DaniWeb® LLC