Hi there,
I am trying to use a login and a contact form on one page, but im struggling with the php context

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
  <title>my webpage</title>
</head>

<body>
<?php
$user = 'admin';
$pass = 'admin';

if ($user != $_POST['username'] || $pass != $_POST['password']) { ?>

<form method="post" >
Enter username:
<input type="text" name="username" /><br/>
Enter Password:
<input type="password" name="password" /><br/>
<center><input type="submit value="login" /></center>

<?php } else { ?>

 <?php 
if ($_POST["email"]<>'') { 
	$ToEmail = 'my email
	$EmailSubject = 'subject
	$mailheader = "From: ".$_POST["email"]."\r\n"; 
	$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
	$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
	$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
	$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
	$MESSAGE_BODY .= "comment".$_POST["comment"]."<br>";

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

<p>Thank you for your comment</p>

<?php 
} else { 
?> <hr/><br/>
       <form method="post">
Enter Name:
    <input type="text" name="name" /><br/>
Enter Email:
    <input type="text" name="email" /><br/>
Comment:
    <input type="textarea" name="comment" /><br/>
<center><input type="submit" name="submit" value="Send" /></center>

<?php }; ?> 

</body>
</html>

if i use that code i get a HTTP 500 INTERNAL SERVER ERROR,
But if i use just this part of the code it all works fine????

<?php 
if ($_POST["email"]<>'') { 
	$ToEmail = 'my email
	$EmailSubject = 'subject
	$mailheader = "From: ".$_POST["email"]."\r\n"; 
	$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
	$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
	$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
	$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
	$MESSAGE_BODY .= "comment".$_POST["comment"]."<br>";

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

<p>Thank you for your comment</p>

<?php 
} else { 
?> <hr/><br/>
       <form method="post">
Enter Name:
    <input type="text" name="name" /><br/>
Enter Email:
    <input type="text" name="email" /><br/>
Comment:
    <input type="textarea" name="comment" /><br/>
<center><input type="submit" name="submit" value="Send" /></center>

<?php }; ?>

any one got any ideas?
I am after creating a contact form but only for people who know the password.

Dani AI

Generated

HTTP 500 almost always means a server-side fatal error — usually a PHP parse error when PHP and HTML are mixed. The immediate fix is to get the real error message. discovered a malformed input attribute later, but the original snippet also shows unclosed PHP strings and missing semicolons which will produce a parse error. Don’t rely on guessing which element broke the page; find the exact line number first.

Enable error reporting during development or check the webserver/PHP error log. Add these lines at the top of the script to see the error and the file/line that triggered it:

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

See the PHP docs on error reporting for details: PHP error_reporting. Another quick check is running php -l yourfile.php on the command line to catch syntax errors, and examine your server error log (e.g., Apache or PHP-FPM logs).

Make the page logic explicit so you can tell which form was submitted and avoid ambiguous POST checks. Name each submit button or include a hidden field and branch on that. Use sessions for login state instead of relying on a single request:

// pattern (illustrative)
if (isset($_POST['login_submit'])) {
  // validate credentials, then session_start(); $_SESSION['logged_in'] = true;
}
elseif (isset($_POST['contact_submit']) && !empty($_SESSION['logged_in'])) {
  // process contact form
}

Harden the contact form: validate email with filter_var($email, FILTER_VALIDATE_EMAIL), sanitize user input, and never trust raw user input in mail headers (or use a library like PHPMailer). See filter_var and mail. Also note ’ good advice to name the submit buttons and to check both username and password together (use && or a direct equality check) so the logic is clear.

Recommended Answers

All 4 Replies

Why did you not include actions in your form tags?

Like:

<form method="post" action="<?php echo $PHP_SELF; ?>">

action="<?php echo $PHP_SELF; ?>" calls the current php file.

Also, why not validate that a button was actually pushed? In your button tag you've got grammatical errors to begin with, and you should name your button.

<input type="submit value="login" />

Should resemble:

<input type="submit" name="buttonName" value="login" />

Like for your first section:

<?php
$user = 'admin';
$pass = 'admin';
if($_POST["buttonName"]){

if ($user != $_POST['username'] || $pass != $_POST['password']) { ?>

<form method="post" action="<?php echo $PHP_SELF; ?> >
Enter username:
<input type="text" name="username" /><br/>
Enter Password:
<input type="password" name="password" /><br/>
<center><input type="submit" name="buttonName" value="login" /></center>

<?php }
} ?>

By the way have you tried just the top of the code by it's lonesome? Try making the changes I mentioned. See if that helps anything.

Another thing I am wondering, the form will not show unless it obtains a value for $_POST to begin with. So try this instead:

<?php
$user = 'admin';
$pass = 'admin';
$status = 0;

if($_POST["buttonName"]){

if (($user != $_POST['username']) && ($pass != $_POST['password'])) 
$status = 0;
else 
status = 1; //accurate data

} //end if($_POST["buttonName"]) 

if ($status == 0) { ?>

<form method="post" action="<?php echo $PHP_SELF; ?>" >
Enter username:
<input type="text" name="username" /><br/>
Enter Password:
<input type="password" name="password" /><br/>
<center><input type="submit" name="buttonName" value="login" /></center>

<?php } ?>

By the way, you should use the and operator, not the or to validate that both pass and user name are valid. Also don't be afraid of brackets, so that code blocks are separated and there's no confusion in the operation to be performed. (line 8 above)

Hope this helps.

Oh, in the last code above, line 11, change status to $status.

:/

i never really got a answer for this, but i have just revisited and found this out,

<center><input type="submit value="login" /></center>

on line 20 is wrong i have missed 1 "
it should read

<center><input type="submit" value="login" /></center>

i cannot belive that i made a simple mistake and scraped the whole project,
and even that none of you spotted it,
for thoses of you who are still trying to work it out here

<center><input type="submit(" <- forgot to put that here) value="login" /></center>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.