ok my assignment is to make a login form that will accept a user name and password. if the user name and password don't match it doesnt do anything, but if it does it takes it to a website. my only problem is when i go to run it i get and error like this
Parse error: syntax error, unexpected $end in /home/bfinnegan/public_html/ta3.php on line 58...i dont know wat it means..need help please

<?php
$user="beatrice";
$pswrd="bean";
$redirect=false;
$max_attempts=false;

$num_attempts=(isset($_POST["num_attempts"])) ? $_POST["num_attempts"] + 1 : 0;
$username = (isset($_POST["username"])) ? $_POST["username"]: "";
if(!isset($_POST["username"]))
{       $msg="Welcome to the Cameron Page!";
}else if($_POST["username"]==$user && $_POST["password"]==$pswrd)//everything matches, move to webpage
{       $redirect=true;
        $msg="";
}else if($num_attempts>10)//kicks user out of program
{       $msg="Exceeded the limit of tries.";
        $max_attempts=true;
}else
{       $msg="Try again. Max number of attempts is 10";
}


$_POST["password"]="";
$guess=$_POST["guess"];

?>
<html>
<head>
<title>Welcome to Cameron</title>
</head>
<?php
if($redirection==true)
{
header("location:http://www.cameron.edu");
exit;
}
?>
</head>
<body>
<strong>
<?php echo $message ?>
</strong>
?>

<?php
if ($max_attempts==false)
{
echo "<form action= ".$_SERVER["PHP_SELF"];?> " method="POST"> ;
echo "User name :<br/><input type="text" name="username" value=".$username"><br/> ";
echo "Password : <br/><input type="password" name="password" value=''><br/> ";
echo "<input type="hidden" name="num_attempts" value=".$num_attempts"> ";
echo "</p> \n";
echo "<p><input type="submit" value="submit your guess"></p>";
}
?>
</form>
</body>
</html>

Dani AI

Generated

A few concrete causes are visible from the errors posted: a stray or misplaced PHP close tag and mismatched quotes inside echo/concatenation were the initial culprits (this is what and pointed out). The later "Unexpected character '\' (ASCII=92)" warnings mean literal backslashes were left in the PHP source (usually from incorrect manual escaping), and "T_CONSTANT_ENCAPSED_STRING" typically means a quoting/concatenation mistake inside an echo. There was also a variable-name mismatch in later snippets ($redirect vs $redirection, $msg vs $message) — such inconsistencies will silently prevent branches (like a redirect) from running.

A pragmatic debugging checklist that addresses all of the above:

  • Run a syntax check: php -l filename.php to catch parse errors quickly.
  • Turn on full error reporting while developing: ini_set('display_errors', 1); error_reporting(E_ALL);.
  • Search for unmatched quotes, unclosed parentheses/braces, and stray ?> tags. Remove any premature ?> that breaks control flow.
  • Simplify HTML output: either close PHP and write the form as plain HTML, or use simple concatenation with consistent quoting. Avoid heavy inline escaping; use single quotes for the outer PHP string and double quotes for HTML attributes (or vice versa), or use heredoc to reduce escaping mistakes.
  • Check redirects: call header('Location: https://www.cameron.edu'); exit; before sending any output (or use output buffering). Also fix any inconsistent variable names so the redirect condition actually evaluates true.
  • Sanitize form action: if using $_SERVER['PHP_SELF'], wrap it in htmlspecialchars(..., ENT_QUOTES) to avoid XSS.

Useful references: PHP header behavior and usage (header() manual) and a quick overview of CLI syntax checking (PHP CLI features). The fixes suggested by and (correct concatenation and removing stray tags) are the right direction; the remaining items above will prevent the escape/backslash and header ordering issues from recurring.

Recommended Answers

All 7 Replies

From what I can tell, it looks as though you have a random php end tag "?>" there on line 42 that doesn't belong. If you clear that out, it might solve that error you're receiving.

IT looks to me like you didn't escape the double quotes, remember that when you have a double quote inside an echo you use \" instead of ". Also, in your code there are a few places where you used an ending ?> tag without the <?php at the beginning. Hope it helps

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/bfinnegan/public_html/ta3.php on line 48

ok i did what you guys advised and i got this error now. i post from
44 to 54

<?php
if ($max_attempts==false)
{
echo "<form action= '".$_SERVER["PHP_SELF"]."' method=\"POST\"> ;
echo "User name :<br/><input type='text' name='username' value='"\.$username\"'><br/> ";
echo "Password : <br/><input type='password' name='password' value=''><br/> ";
echo "<input type='hidden' name='num_attempts' value='.$num_attempts'> ";
echo "</p> \n";
echo "<p><input type=\"submit\" value=\"submit your guess\"></p>";
}
?>

Alright, I just took what you originally had there for those lines and added the escapes like adyopo mentioned. You just missed a few with your second posting. For example, on line 4 (of your second post) you need quotation marks at the very end before the semi-colon. On lines 5 and 7 it appears you try to concatenate the values with the string, however you put the period, '.' only before the value. Take a look at the following, I think it should work...

<?php
	if ($max_attempts==false)
	{
		echo "<form action= " . $_SERVER["PHP_SELF"] . " method=\"POST\">";
		echo "User name :<br/><input type=\"text\" name=\"username\" value=\"" . $username . "\"><br /> ";
		echo "Password : <br/><input type=\"password\" name=\"password\" value=''><br />";
		echo "<input type=\"hidden\" name=\"num_attempts\" value=\"" . $num_attempts . "\">";
		echo "</p> \n";
		echo "<p><input type=\"submit\" value=\"submit your guess\"></p>";
	}
?>

ok well i did make the changes and here are the errors i got.
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/bfinnegan/public_html/ta3.php on line 48

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/bfinnegan/public_html/ta3.php on line 48

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/bfinnegan/public_html/ta3.php on line 50

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/bfinnegan/public_html/ta3.php on line 50

this is confusing

ok never mind. i got it to work! thank you so much for the help!

try this code

<?php
$user="beatrice";
$pswrd="bean";
$redirect=false;
$max_attempts=false;
$num_attempts=(isset($_POST["num_attempts"])) ? $_POST["num_attempts"] + 1 : 0;
$username = (isset($_POST["username"])) ? $_POST["username"]: "";
if(!isset($_POST["username"]))
{ $msg="Welcome to the Cameron Page!";
}else if($_POST["username"]==$user && $_POST["password"]==$pswrd)//everything matches, move to webpage
{ $redirect=true;
$msg="";
}else if($num_attempts>10)//kicks user out of program
{ $msg="Exceeded the limit of tries.";
$max_attempts=true;
}else
{ $msg="Try again. Max number of attempts is 10 & it's your ".$num_attempts." attempt" ;
}
$_POST["password"]="";
//$guess=$_POST["guess"];
?>
<html>
<head>
<title>Welcome to Cameron</title>
</head>
<?php
if($redirect == true)
{
header("location:http://www.cameron.edu");
exit;
}
?>
</head>
<body>
<strong>
<?php echo $msg; ?>
</strong>
<?php
if ($max_attempts==false)
{
echo "<form action= ".$_SERVER["PHP_SELF"]." method=\"POST\">";
echo "User name :<br/><input type=\"text\" name=\"username\" value=\"".$username."\"><br/>";
echo "Password : <br/><input type=\"password\" name=\"password\" value=\"\"><br/>";
echo "<input type=\"hidden\" name=\"num_attempts\" value=\"".$num_attempts."\">";
echo "</p> \n";
echo "<p><input type=\"submit\" value=\"submit your guess\"></p>";
}
  ?>
</form>
</body>
</html>
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.