Reputation: 5229
Hey guys, I have this piece of code, and when I add return after echo(if there is an error and I need to continue right after the script) I can't see the footer, do you know what the problem is?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" >
<head>
<title>Login | JM Today </title>
<link href="Mainstyles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<?php include("header.php"); ?>
<?php include("navbar.php"); ?>
<?php include("cleanquery.php") ?>
<div id="wrap">
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$conn=mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db('jmtdy', $conn) or die(mysql_error());
if(isset($_POST['sublogin'])){
if(( strlen($_POST['user']) >0) && (strlen($_POST['pass']) >0)) {
checklogin($_POST['user'], $_POST['pass']);
}
elseif((isset($_POST['user']) && empty($_POST['user'])) || (isset($_POST['pass']) && empty($_POST['pass']))){
echo '<p class="statusmsg">You didn\'t fill in the required fields.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
}
else{
echo '<p class="statusmsg">You came here by mistake, didn\'t you?</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
function checklogin($username, $password){
$username=mysql_real_escape_string($username);
$password=mysql_real_escape_string($password);
$result=mysql_query("select * from users where username = '$username'");
if($result != false){
$dbArray=mysql_fetch_array($result);
$dbArray['password']=mysql_real_escape_string($dbArray['password']);
$dbArray['username']=mysql_real_escape_string($dbArray['username']);
if(($dbArray['password'] != $password ) || ($dbArray['username'] != $username)){
echo '<p class="statusmsg">The username or password you entered is incorrect. Please try again.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if(isset($_POST['remember'])){
setcookie("jmuser",$_SESSION['username'],time()+60*60*24*356);
setcookie("jmpass",$_SESSION['username'],time()+60*60*24*356);
}
}
else{
echo'<p class="statusmsg"> The username or password you entered is incorrect. Please try again.</p><br/>input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
}
?>
</div>
<br/>
<br/>
<?php include("footer.php") ?>
</div>
</body>
</html>
Oh, and if the username and password is incorrect I could see the footer.
Upvotes: 0
Views: 6048
Reputation: 11
Try to insert your php part as a php include. The return; statement will only stop the execution of the included php and it will continue the rest after the included.
<?php include("footer.php") ?>
will output the footer.
Upvotes: 1
Reputation: 1257
Try to remove "return" (leave it only in functions) nothing will change in this case, but you'll see you footer.
Upvotes: 1
Reputation:
You can't exit out of a script and continue running the same script.
But some advice about your return statements:
1) If your function doesn't return anything, then don't put a return statement in there.
2) Use only one return statement per function, it will make your code more clear.
Upvotes: 1
Reputation: 898
When you have a return statement and you are not inside a function, the script will exit, thus the parser never reaches the statement that imports the footer. Keep in mind your script is the entire document, not just the parts enclosed in tags. If the parser stops somewhere, the entire rest of the document is left out.
Upvotes: 2