PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/login.php

https://github.com/MeeC/akinita
PHP | 77 lines | 63 code | 10 blank | 4 comment | 8 complexity | a26e0ab601afffe5c967535a84fe1b5f MD5 | raw file
  1. <?php
  2. session_start();
  3. if (isset($_POST['userid']) && isset($_POST['password']))
  4. {
  5. // if the user has just tried to log in
  6. $userid = $_POST['userid'];
  7. $password = $_POST['password'];
  8. $db_conn = mysql_connect("localhost", "akinauth", "password");
  9. if (!$db_conn) {
  10. echo 'Connection to database failed:'.mysql_error();
  11. exit();
  12. }
  13. $db_selected = mysql_select_db("akinita", $db_conn);
  14. if (!$db_selected)
  15. {
  16. die ("Can\'t use test_db : " . mysql_error());
  17. }
  18. $result = mysql_query("SELECT * FROM users where username='$userid' and password='$password'");
  19. $num_results=mysql_num_rows($result);
  20. if ($num_results>0)
  21. {
  22. $_SESSION['valid_user'] = $userid;
  23. }
  24. else
  25. {
  26. echo 'problem........could not log you in...did you use the correct username/password ?!';
  27. }
  28. mysql_close($db_conn);
  29. }
  30. ?>
  31. <html>
  32. <body>
  33. <h1>Home page</h1>
  34. <?
  35. if (isset($_SESSION['valid_user']))
  36. {
  37. echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />';
  38. echo '<a href="logout.php">Log out</a><br />';
  39. }
  40. else
  41. {
  42. if (isset($userid))
  43. {
  44. // if they've tried and failed to log in
  45. echo 'Could not log you in.<br />';
  46. }
  47. else
  48. {
  49. // they have not tried to log in yet or have logged out
  50. echo 'You are not logged in.<br />';
  51. }
  52. // provide form to log in
  53. echo '<form method="post" action="login.php">';
  54. echo '<table>';
  55. echo '<tr><td>Userid:</td>';
  56. echo '<td><input type="text" name="userid"></td></tr>';
  57. echo '<tr><td>Password:</td>';
  58. echo '<td><input type="password" name="password"></td></tr>';
  59. echo '<tr><td colspan="2" align="center">';
  60. echo '<input type="submit" value="Log in"></td></tr>';
  61. echo '</table></form>';
  62. }
  63. ?>
  64. <br />
  65. <a href="members_only.php">Members section</a>
  66. </body>
  67. </html>