User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 403,300 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,793 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 343 | Replies: 9 | Solved
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Login problem, need fresh set of eyes.

  #1  
May 5th, 2008
Hey guys, i've got a right headache from this piece of code at the moment, it's a simle login script and it just sin't working, the MySQL query looks correct to me and all the other stuff looks ok, but then again i'm no PHP expert.

Could someone look at it for me please?
  1. <?
  2. include ('./includes/header.php');
  3. // Check if the form has been submitted.
  4. if (isset($_POST['submitted'])) {
  5. require_once('../sqlconnect/connect.php');
  6.  
  7. $errors = array(); // Initialize error array.
  8. // Check for an email address.
  9. if (empty($_POST['email'])) {
  10. $errors[] = 'You forgot to enter your email address.';
  11. } else {
  12. $em = trim($_POST['email']);
  13. }
  14. // Check for a password.
  15. if (empty($_POST['pass'])) {
  16. $errors[] = 'You forgot to enter your password.';
  17. } else {
  18. $pw = trim($_POST['pass']);
  19. }
  20. if (empty($errors)) { // If everything's OK.
  21.  
  22. $query = "SELECT * FROM members WHERE email = '$em' AND password = SHA('$pw')";
  23.  
  24. $result = @mysql_query($query);
  25. // Run the query.
  26. $row = mysql_fetch_array ($result, MYSQL_NUM);
  27.  
  28. // Return a record, if applicable.
  29. if ($row){ // A record was pulled from the database.
  30.  
  31. //set session
  32. session_name('visit');
  33. session_start();
  34. $_SESSION ['id'] = $row[0];
  35. $_SESSION ['name'] = $row[1];
  36. $_SESSION ['email'] = $row[3];
  37. $_SESSION ['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
  38.  
  39. // Redirect the user to the loggedin.php page.
  40. // Start defining the URL.
  41. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
  42. // Check for a trailing slash.
  43. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
  44. $url = substr ($url, 0, -1); // Chop off the slash.
  45. }
  46. // Add the page.
  47. $url .= '/loggedin.php?' . $_SESSION['agent'];
  48.  
  49. header("Location: $url");
  50. exit();
  51. // Quit the script.
  52. } else { // No record matched the query.
  53. $errors[] = 'The email address and password entered do not match those on file.'; // Public message.
  54. $errors[] = mysql_error() . '<br />Query: ' . $query; // Debugging message.
  55. }
  56.  
  57. } // End of if (empty($errors)) IF.
  58.  
  59. mysql_close(); // Close the database connection.
  60.  
  61. } else { // Form has not been submitted.
  62.  
  63. $errors = NULL;
  64.  
  65. } // End of the main Submit conditional.
  66.  
  67. //print errors
  68. if (!empty($errors)){
  69.  
  70. echo '<h1 id = mainhead>Error!</h1>
  71. <p class = error >Following occured:<br/>';
  72.  
  73. foreach ($errors as $msg){
  74. echo " - $msg<br/>\n";
  75. }
  76. echo '<p>Please try again <a href = login.php>Reset</a>';
  77. }
  78. ?>

Cheers
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC