Page 1 of 1

[SOLVED /W HELP FROM TIMVW] Help needed with a password form

Posted: Wed Jul 27, 2005 8:13 am
by pilau
Take a look at this code:

Code: Select all

<?php
  // THIS PART IS A SWITCH THAT DECIDES IF TO PRINT THE FORM OR NOT.
  if (!isset($_POST['flag'])) {$flag = 0;}
   else {$flag = 1;}
  // end of switch

   // FORM
   if ($flag == 0) {

   $html = <<<EOT
    <form action="test.php" method="POST">
     Username: <input type="text" name="username" size="30">
      <br>
     Password: <input type="password" name="password" size="30">
      <br>
     <input type="hidden" name="flag" value="1">
     <input type="submit" value="yo">
    </form>
EOT;

    print $html;
   }
   // end of form

   // HERE STARTS THE "REAL" PHP CODE, WHERE I CONNECT TO THE DB AND
   // CHECK THAT THE PASSWORDS MATCH
  else {
   $flag = 0; // THIS IS SUPPOSED TO LOOP THE FILE BACK TO THE FORM
    mysql_connect("***","***","***");
    mysql_select_db("***");
    $result = mysql_query("SELECT * FROM ***");
    $arr = mysql_fetch_assoc($result);

   if ($arr['pass'] === $_POST['password']) {
     print "yes!";
   }

   else {
     print "no!";
   }

  }
   // IF THE PASSWORDS MATCH IT OUTPUTS "YES", OTHERWISE: "NO".
?>
This code check for a password match between one that is user entered and one that is within a table on my MySQL DB. Basically, it works, but I want it to go back to the form after I press "refresh" on the page. The document will process the form code only if $flag is equal to 0.

Posted: Wed Jul 27, 2005 8:35 am
by nielsene
When you refresh you resend the post data so it thinks you've resubmitted the form. You might want to look at the newest "Code Snippet" for a RelativeRedirect tool. After running the script, do a redirect back to the own page. That will clear the variable for you without posting.

Posted: Wed Jul 27, 2005 8:37 am
by pilau
What does the header() function does?

Posted: Wed Jul 27, 2005 8:39 am
by nielsene
It sends "special" commands to the browser. In this case it causes the browser to request a new page.

Posted: Wed Jul 27, 2005 8:47 am
by pilau
Sweet. I'm going to read about it more on the internet. Thanks you two.

Posted: Thu Jul 28, 2005 1:35 am
by pilau
Sorry for double posting, but I used the relativeRedirect function on the page, and it just didn't work. I tried using it in different ways, for example putting that function in a single php document that would automatically redirect to another page, and that worked, but when I added it to my piece of code, it just didn't work. Take a look:

Code: Select all

<?php
include "redirect.php";  //See Code Snippets forum.

  // THIS PART IS A SWITCH THAT DECIDES IF TO PRINT THE FORM OR NOT.
  if (!isset($_POST['flag'])) {$flag = 0;}
   else {$flag = 1;}
  // end of switch

   // FORM
   if ($flag == 0) {

   $html = <<<EOT
    <form action="test.php" method="POST">
     Username: <input type="text" name="username" size="30">
      <br>
     Password: <input type="password" name="password" size="30">
      <br>
     <input type="hidden" name="flag" value="1">
     <input type="submit" value="yo">
    </form>
EOT;

    print $html;
   }
   // end of form

   // HERE STARTS THE "REAL" PHP CODE, WHERE I CONNECT TO THE DB AND
   // CHECK THAT THE PASSWORDS MATCH
  else {
   $flag = 0; // THIS IS SUPPOSED TO LOOP THE FILE BACK TO THE FORM
    mysql_connect("***","***","***");
    mysql_select_db("***");
    $result = mysql_query("SELECT * FROM ***");
    $arr = mysql_fetch_assoc($result);

   if ($arr['pass'] === $_POST['password']) {
     print "yes!";
   }

   else {
     print "no!";
   }

  }
   // IF THE PASSWORDS MATCH IT OUTPUTS "YES", OTHERWISE: "NO".

   relativeRedirect("test.php");
?>

Posted: Thu Jul 28, 2005 4:24 am
by timvw
prepend the following to your code

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
In all the cases you print something.. And then send out the header...
As explained on http://www.php.net/header this is not allowed.

Posted: Thu Jul 28, 2005 5:09 am
by pilau
Understood. Header should be first. So what to do?
Again, I want the page to redirect to itself.
(Like they do in forums when you login).

Posted: Thu Jul 28, 2005 10:14 am
by pilau
Sorry again for double posting, but I really need to solve this problem!!
I want the page to redirect to itself. ,like they do in forums when you login.
How do you do that?

Posted: Thu Jul 28, 2005 10:19 am
by timvw
Although its against my view on KISS (each page/script does one task, fe: list, update, delete, create all deserve their own script.)

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', FALSE);

session_start();

if (!empty($_SESSION['user_id']))
{
  // display stuff when user is logged in
}
else if (isset($_POST['user_id']) && isset($_POST['password']))
{
  // test if user is valid
  if (validate())
  {
    $_SESION['user_id'] = $_POST['user_id'];
  }
  // redirect 
  redirect($_SERVER['PHP_SELF']);
}
else
{
  // display login form
}

Posted: Thu Jul 28, 2005 10:24 am
by pilau
Thanks. All I want is just to add a login form to MyPHPAdmin!

Posted: Thu Jul 28, 2005 11:38 am
by timvw
What is wrong with the one in phpmyadmin itself??

Posted: Thu Jul 28, 2005 12:06 pm
by pilau
There isn't one :S
When I try to access index.php it just lets me in without any request of password or username or anything.

I thought it was odd at first but then I figured hell, if I'm learning PHP why should't I code one myself?
So I did.

And thanks to you, it actually worked!