PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?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.
Last edited by pilau on Thu Jul 28, 2005 12:29 pm, edited 4 times in total.
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.
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:
<?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");
?>
Last edited by pilau on Thu Jul 28, 2005 5:19 am, edited 2 times in total.
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?
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
}