I am using a code from the book 'PHP & MySQL for Dynamic Websites' by Larry Ullman (Visual QuickPro Guide) for my login page.

I have followed the code for the queries without any problem, but it sets the form action for the username / password form to the following ;
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

When I run this form it comes up with error HTTP 404 File Not Found.

I understand that the form action basically send the user to a page location, and I have not got this page located on my server, so I am getting the error message, but I don't understand what file I am supposed to be saving, where to save it, and what code needs to be on this file to handle to form and take the user to the page I have specified in the header ("location: ......") part of my code.

The full code is shown below......
any ideas? thanks

<?php

if (isset($_POST['submit']))
// Check if the form has been submitted
{
require_once ('../mysql_connect.php');
//connect to the database

if (empty($POST['submit']))
// validate the username
{ $username = FALSE;
echo '<p><font color="red"> You forgot to enter your username</font></p>';
}
else
{
$username = escape_data($
POST['username']);
}
if (empty($POST['password'])) // validate the password
{ $password = FALSE;
echo '<p><font color="red"> You forgot to enter your password</font></p>';
}
else
{
$password = escape_data($
POST['password']);
}

if ($username && $password) // everything is OK
{ // query the database

$query = "SELECT CLogIn FROM tblUser

WHERE CLogIn='$username'
AND CPass=PASSWORD('$password')";
$result = @($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);

if ($row) // username and password match database records

{ // start the session, register the values, and redirect
$SESSION['username'] = $row[1];
$
SESSION['user_id'] = $row[0];

header ("Location: [url]http://[/url]" . $SERVER['HTTP_HOST'] . dirname($SERVER['PHP_SELF'] . "/equipment.php");
exit();

}
else // no match was made
{
echo '<p><font color="red">The Username and Password you entered do not match those on file.</font></p>';
}
}
else // if everything wasn't OK
{
echo '<p><font color="red">Please try again</font></p>';
}

}

?>
<?php
include ('header.php');
?>

<br>
<h1><font face="Arial, Helvetica, sans-serif">Login</font></h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset><legend><font face="Arial, Helvetica, sans-serif">Please Enter Your Information Below;</font></legend>

<p><font face="Arial, Helvetica, sans-serif" size="3"> <b>Username:</b>
<input type="text" name="username" size="30" maxlength="60" value="<?php if(isset($POST['username'])) echo $POST['username']; ?>" />
</font></p>

<p><font face="Arial, Helvetica, sans-serif" size="3"> <b>Password:</b>
<input type="password" name="password" size="30" maxlength="60" />
</font></p><br>

</fieldset>

<div align="center"> <input type="submit" name="submit" value="Submit" /></div>

</form>

    either check your source code of the generated page or do an echo of the $_SERVER['PHP_SELF'] just to check that it is, in fact, giving you the right page name. that'd be my first port of call...

      Sorry, not got a clue what you just said
      when I say I am a newbie, I mean very very new newbie,
      can you put it in idiot terms !!!!


      I have just tried running
      <?php
      echo $SERVER_['PHP_SELF']
      ?>
      and I get a blank page .........
      what should I get, I don't know what to expect.

      thanks

        well in theory you should get a page with whatever the value of $SERVER['PHP_SELF'] (note the goes before the SERVER, not after it) - if it's not outputting anything, then the variable is empty, which would be why the form isn't submitting to itself - it's not getting the name of the page.

        I've always had problems with the $_SERVER commands, so have given up on using them. Why don't you just put the name of the file in the action field?

          Or you can not use action and the page will relaod. Buty better put action="filename.php" and will work just fine๐Ÿ˜‰

            Generally $_SERVER['PHP_SELF']would be available unless you're using the command line.

            However, what version of PHP are you using? Before PHP 4.1.0, you have to use the old style reserved words.

              thanks everyone ...

              I have changed the header (location: [url]http://[/url]" . $SERVER etc etc etc to read the standard URL for the page and changed the form action to read just the page name (instead of $SERVER etc). and it now works OK.

              the form doesn't come back on itself anymore (but that isn't a problem for what I am doing)

              couldn't do it without you guys :p

                Write a Reply...