Connecting Tech Pros Worldwide Forums | Help | Site Map

login php

Member
 
Join Date: Apr 2009
Posts: 63
#1: May 18 '09
i would like to create php login form but the problem is the form doesnt do anything
Expand|Select|Wrap|Line Numbers
  1. <?php require_once("nocache.php"); ?>
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  5.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  6. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  7. <head>
  8.     <title>login.php</title>
  9. <?php
  10. if(isset($_POST['Submit']))
  11.     if (empty($id) || empty($pword)){
  12.         header("location: login.php");
  13.         }else{
  14.             $conn = mysql_connect("localhost", "TWAassign2", "student");
  15.             if(!$conn){
  16.                 die('Conection could not be opened: ' . mysql_error());
  17.             }
  18.             $db_selected = mysql_select_db('acadcredit', $conn);
  19.             $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  20.             $rs = mysql_query($sql, $conn);
  21.             if (mysql_num_rows($rs)> 0 ) {
  22.                 session_start();
  23.                 $_SESSION["who"] = $id;
  24.                 $_SESSION["level"] = mysql_result($rs,0,"level");
  25.                 header("location: selectView.php");
  26.             }else{
  27.                 header("location: login.php");
  28.             }
  29.         }
  30. ?>
  31.  
  32. </head>
  33. <body>
  34. <form method="post" action="login.php">
  35. <h1> Welcome to Login Page </h1>
  36. <h2> Please enter Your Staff ID and password- Thanks</h2>
  37.     <p>Staff ID  <input type="text" name="ID"  /><br/>
  38.     Password <input type="password" name="pass" /></p> 
  39. <p> 
  40. <?php
  41.     if(isset($_POST['ID'])){
  42.         $id = $_POST["ID"];
  43.         $pword = $_POST["pass"];
  44.     }
  45. ?>
  46.  
  47.     <input type="submit" value="login" />
  48.     <input type="reset"  value="Reset" /></p>
  49. </form>
  50. </body>
  51. </html>
  52.  
why isnt working
thanks

prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#2: May 18 '09

re: login php


Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php session_start();
  3. // have to initialised session_start(); top of the page 
  4. ?>
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  7. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  8. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  9. <head>
  10. <title>login.php</title>
  11. <?php
  12. if(isset($_POST['Submit'])){
  13.  
  14.     $id= $_POST['ID'];
  15.     $pword= $_POST['pass'];
  16.  
  17. }
  18. if (empty($id) || empty($pword)){
  19. header("location: login.php");
  20. }else{
  21. $conn = mysql_connect("localhost", "TWAassign2", "student");
  22. if(!$conn){
  23. die('Conection could not be opened: ' . mysql_error());
  24. }
  25. $db_selected = mysql_select_db('acadcredit', $conn);
  26. $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  27. $rs = mysql_query($sql, $conn);
  28. if (mysql_num_rows($rs)> 0 ) {
  29. session_start();
  30. $_SESSION["who"] = $id;
  31. $_SESSION["level"] = mysql_result($rs,0,"level");
  32. header("location: selectView.php");
  33. }else{
  34. header("location: login.php");
  35. }
  36. }
  37. ?>
  38.  
  39. </head>
  40. <body>
  41. <form method="post" action="login.php">
  42. <h1> Welcome to Login Page </h1>
  43. <h2> Please enter Your Staff ID and password- Thanks</h2>
  44. <p>Staff ID <input type="text" name="ID" /><br/>
  45. Password <input type="password" name="pass" /></p> 
  46. <p> 
  47.     <!-- this pice of code goes top of the page then tou check $_POST['submit'] when you submit the page then passing $id and $pword would be
  48.     initialised,
  49.  
  50.      -->
  51. <?php
  52. /*
  53. if(isset($_POST['ID'])){
  54. $id = $_POST["ID"];
  55. $pword = $_POST["pass"];
  56. }*/
  57. ?>
  58.  
  59. <input type="submit" value="login" />
  60. <input type="reset" value="Reset" /></p>
  61. </form>
  62. </body>
  63. </html>
  64.  
  65.  
  66.  
Member
 
Join Date: Apr 2009
Posts: 63
#3: May 18 '09

re: login php


i did what you said but still doesnt do anything not giving error or goes to next page and doesnt submit to itself so whats wrong with it
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#4: May 18 '09

re: login php


try this and if any error come mention that
Expand|Select|Wrap|Line Numbers
  1. <?php session_start();
  2. // have to initialised session_start(); top of the page 
  3. ?>
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head>
  7. <title>login.php</title>
  8. </head>
  9. <body >
  10.     <br><br>
  11.  
  12.  
  13.  <?
  14.  
  15.  if(isset($_POST['submit'])){
  16.  
  17.     $id= $_POST['ID'];
  18.     $pword= $_POST['pass'];
  19.     /echo "password".$pword;
  20.  //echo "id".$id;
  21. }
  22.  if (empty($id) || empty($pword)){
  23. header("location: login.php");
  24. }else{
  25. $conn = mysql_connect("localhost", "TWAassign2", "student");
  26. if(!$conn){
  27. die('Conection could not be opened: ' . mysql_error());
  28. }
  29. $db_selected = mysql_select_db('acadcredit', $conn);
  30. $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  31. $rs = mysql_query($sql, $conn);
  32. if (mysql_num_rows($rs)> 0 ) {
  33.  
  34. $_SESSION["who"] = $id;
  35. $_SESSION["level"] = mysql_result($rs,0,"level");
  36. header("location: selectView.php");
  37. }else{
  38. header("location: login.php");
  39. }
  40. }
  41.  ?>
  42.   <!--=============================================================================-->
  43.  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  44. <h1> Welcome to Login Page </h1>
  45. <h2> Please enter Your Staff ID and password- Thanks</h2>
  46. <p>Staff ID <input type="text" name="ID" /><br/>
  47. Password <input type="password" name="pass" /></p> 
  48.  
  49. <input type="submit" value="login" />
  50. <input type="reset" value="Reset" /></p>
  51. </form>
  52.  
  53.  
  54.  
  55. </body>
  56. </html>
  57.  
  58.  
  59.  
  60.  
Member
 
Join Date: Apr 2009
Posts: 63
#5: May 18 '09

re: login php


the error was no input satisfied and then page not found why? and i have this at the top of page0 ) { $_SESSION["who"] = $id; $_SESSION["level"] = mysql_result($rs,0,"level"); header("location: selectView.php"); }else{ header("location: login.php"); } } ?> please help thanks
Member
 
Join Date: Apr 2009
Posts: 63
#6: May 19 '09

re: login php


i realy need help to create the login page as soon as possible please help
thanks
Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#7: May 19 '09

re: login php


creating a login is really simple:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. if(!isset($_SESSION)){
  3. session_start();
  4. }
  5.  
  6. if($_SERVER['REQUEST_MEHOD']='post'){
  7. $conn = mysql_connect("myServer","root","");
  8. mysql_select_db("myDB");
  9.  
  10. $result = mysql_query("SELECT * FROM users WHERE user = ".$_POST['username']." AND password = ".$_POST['passwd'],$conn);
  11.  
  12. if(mysql_num_rows($result) == 1){
  13. $_SESSION['user']=$_POST['username'];
  14. $_SESSION['pw']=$_POST['passwd'];
  15.  
  16. header('location: anotherpage.php');
  17. } else {
  18. echo 'failed to login';
  19. }
  20. }
  21.  
  22. mysql_free_result($result);
  23. mysql_close($conn);
  24. ?>
  25.  
  26. <form method='post'>
  27. <input type='text' id='username' name='username' />
  28. <input type='password' id='passwd' name='passwd' />
  29. <input type='submit' />
  30. </form>
on all other pages you use exacly the same code (appart from the form) but instead you use your session variables in your query. also, when you see the SQLquery returns more then one or no row, you use
Expand|Select|Wrap|Line Numbers
  1. header('location: login.php');
in your if structure

what did you do wrong:
your first try came very close. you only forgot that php is executed before page loads so this
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(isset($_POST['ID'])){
  3.         $id = $_POST["ID"];
  4.         $pword = $_POST["pass"];
  5.     }
  6. ?>
needs to be checked in the beginning instead of at the end of your page.

hope that helped

jan
Member
 
Join Date: Apr 2009
Posts: 63
#8: May 19 '09

re: login php


i did what you said and i have these errors Notice: Undefined index: username in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 18
Notice: Undefined index: passwd in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 18
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 20
failed to login
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head>
  8.     <title>login.php</title>
  9.     <?php
  10. if(!isset($_SESSION)){
  11. session_start();
  12. }
  13.  
  14. if($_SERVER['REQUEST_METHOD']='post'){
  15. $conn = mysql_connect("localhost", "TWAassign2", "student");
  16. $db_selected = mysql_select_db('acadcredit', $conn);
  17.  
  18.  
  19. $result = mysql_query("SELECT staffName accessLevel  FROM users WHERE staffID= ".$_POST['username']." AND password = ".$_POST['passwd']);
  20.  
  21. if(mysql_num_rows($result) == 1){
  22. $_SESSION['user']=$_POST['username'];
  23. $_SESSION['pw']=$_POST['passwd'];
  24.  
  25. header('location: selectView.php');
  26. } else {
  27. echo 'failed to login';
  28. }
  29. }
  30.  
  31. mysql_free_result($result);
  32. mysql_close($conn);
  33. ?>
  34. </head>
  35. <body>
  36. <form method='post'>
  37. <p><input type='text' id='username' name='username' /> <br/>
  38. <input type='password' id='passwd' name='passwd' /></p>
  39. <p> <input type='submit' /> </p><input type="reset"  value="Reset" />
  40. </form>
  41. </body>
  42. </html>
  43.  
i need help please
Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#9: May 19 '09

re: login php


ok my fault i think. try this:
Expand|Select|Wrap|Line Numbers
  1. if($_SERVER['REQUEST_METHOD'] == 'post'){
i forgot to use double 'equals' :S
Reply