Redirect php after log-in only does "div"

Hi, I am trying to build an admin section for my website which has a log in form on the homepage which is in an html “div” element. If I use header() it redirects to the page but only in the div element which contains the form. How do I get it so that it redirects/changes the whole page to adminarea.php lets say. The code including the div element surrounding it is below.

[php]

<?php session_start(); // dBase file include "dbConfig.php"; if ($_GET["op"] == "login") { if (!$_POST["username"] || !$_POST["password"]) { die("You need to provide a username and password."); } // Create query $q = "SELECT * FROM `dbUsers` " ."WHERE `username`='".$_POST["username"]."' " ."AND `password`=PASSWORD('".$_POST["password"]."') " ."LIMIT 1"; // Run query $r = mysql_query($q); if ( $obj = @mysql_fetch_object($r) ) { // Login good, create session variables $_SESSION["valid_id"] = $obj->id; $_SESSION["valid_user"] = $_POST["username"]; $_SESSION["valid_time"] = time(); // Redirect to member page Header("Location: adminarea.php"); } else { // Login not successful die("Sorry, could not log you in. Wrong login information."); } } else { //If all went right the Web form appears and users can log in echo ""; echo "Username:
"; echo "Password:
"; echo ""; echo ""; } ?>
  </div></div>[/php]

Normally redirecting with header() would reload entire page, not just part of it (div etc.)
Are you using frames or iframes?

No Im not using frames or iframes at all. Have just uploaded the page and it is here if it helps: http://www.mershamlehatchcc.co.uk/trial3php.php
Thanks

I see. You need to avoid using die() within document - this will skip the rest of html code, below this command. So, instead of:
[php]die(“Sorry, could not log you in. Wrong login information.”);[/php]
have some variable set at this point, and output it later using echo. For example:
[php]

<?php session_start(); // dBase file include "dbConfig.php"; $errmsg = ''; // here we will store error message if ($_GET["op"] == "login") { if (!$_POST["username"] || !$_POST["password"]) { $errmsg = "You need to provide a username and password."; } else{ // Create query $q = "SELECT * FROM `dbUsers` " ."WHERE `username`='".$_POST["username"]."' " ."AND `password`=PASSWORD('".$_POST["password"]."') " ."LIMIT 1"; // Run query $r = mysql_query($q); if ( $obj = @mysql_fetch_object($r) ) { // Login good, create session variables $_SESSION["valid_id"] = $obj->id; $_SESSION["valid_user"] = $_POST["username"]; $_SESSION["valid_time"] = time(); // Redirect to member page Header("Location: adminarea.php"); exit; } else { // Login not successful $errmsg = "Sorry, could not log you in. Wrong login information."; } } else { //If all went right the Web form appears and users can log in echo ""; echo "Username:
"; echo "Password:
"; echo ""; echo ""; } } if($errmsg != '') echo $errmsg; ?>
  </div></div>[/php]

I know get an error (Parse error: syntax error, unexpected T_ELSE in /homepages/42/d337817618/htdocs/trial3php.php on line 56) How does it now show an error on the else statement even though this has not been changed, any ideas?

OK I have now solved that problem but it still does not redirect to the right page, it now just produces the header banner and nothing else. This is the url that it ends up with once i click log-in (im new to php and mysql so is this correct?): http://www.mershamlehatchcc.co.uk/trial3php.php?op=login.

This is the php code at the moment:
[php] <?php
session_start();
// dBase file
include “dbConfig.php”;

    $errmsg = 'Error Occured on Login';   // here we will store error message

    if ($_GET["op"] == "login")

{
if (!$_POST[“username”] || !$_POST[“password”])
{
$errmsg = “You need to provide a username and password.”;
}
else{
// Create query
$q = “SELECT * FROM dbUsers "
.“WHERE username=’”.$_POST[“username”].”’ "
.“AND password=PASSWORD(’”.$_POST[“password”]."’) "
.“LIMIT 1”;
// Run query
$r = mysql_query($q);

if ( $obj = @mysql_fetch_object($r) )
{
// Login good, create session variables
$_SESSION[“valid_id”] = $obj->id;
$_SESSION[“valid_user”] = $_POST[“username”];
$_SESSION[“valid_time”] = time();

    // Redirect to member page
    Header("Location: memberstrial.php");
    exit;
    }

else
{
// Login not successful
$errmsg = “Sorry, could not log you in. Wrong login information.”;
}
}
}
else
{
//If all went right the Web form appears and users can log in
echo “<form action=”?op=login" method=“POST”>";
echo “Username: <input name=“username” size=“15”>
”;
echo “Password: <input type=“password” name=“password” size=“8”>
”;
echo “<input type=“submit” value=“Login”>”;
echo “”;
}

if($errmsg != ‘’)
{
echo $errmsg;
}
?>[/php]

It looks like your script still produce some error/warning message and not redirect properly. Make sure you have no any space or other chars above the opening <?php Note, there must be no any output to browser before you send headers with header() function. In the current code what you posted, I can see some spaces before opening <?php at the very top. You need to eliminate this.

Sponsor our Newsletter | Privacy Policy | Terms of Service