PHP Login Script

DealthRune DealthRune is offline Offline Aug 22nd, 2009, 1:07 pm |
0
Here is a simple login script
Quick reply to this message  
PHP Syntax
  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. ?>
0
vijaysankarbhat vijaysankarbhat is offline Offline | Sep 22nd, 2009
great!! what an effort wonderful!!
 
0
kkeith29 kkeith29 is offline Offline | Sep 23rd, 2009
SQL injection holes. Not secure at all. I wouldn't use it.
 
-1
phong1040572 phong1040572 is offline Offline | Sep 29th, 2009
it's not useful. SQL injection.!
 
0
sureronald sureronald is offline Offline | Sep 30th, 2009
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!
 
0
ayesha789 ayesha789 is offline Offline | Sep 30th, 2009
nice ? but how we can add secret pages for different users
 
0
leviathan185 leviathan185 is offline Offline | Sep 30th, 2009
mysql_real_escape_string(); will prevent injections.

e.g.

  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
 
1
kkeith29 kkeith29 is offline Offline | Sep 30th, 2009
If you are wanting a better login script look here:

http://www.daniweb.com/forums/post95...tml#post951182
 
0
cwarn23 cwarn23 is offline Offline | Oct 1st, 2009
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.
  1. <?php
  2. $data = mysql_real_escape_string(stripslashes($_POST['data']));
  3. ?>
 
-1
jalaladdin jalaladdin is offline Offline | Oct 12th, 2009
please help me about php
 
 

Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC