[SOLVED] newb : header() question/issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

[SOLVED] newb : header() question/issue

Post by hurdy gurdy »

Hi all,

I'm having some issues with the header() function. All I want it to do is redirect to a new page after someone successfully logs in. Attached below is the code and error. I would appreciate any help or info on this.

Thanks


*snipped*

Code: Select all

<?
  $login = $_POST['login'];
  $pass = $_POST['pass'];
	
  if ( !isset($login) && !isset($pass) ) {	
?>

<form action="index.php" method="post">
login : <input type="text" name="login" size="30"><br>
password : <input type="password" name="pass" size="30"><br>
<input type="submit" value="login">
</form>

<?
  } else {
    $qryLogin = mysql_query ("SELECT strUserName, strUserPass FROM TableUser WHERE strUserName = '$login'");
    $loginResult = mysql_fetch_array ($qryLogin);

    if ( ($loginResult[strUserName] == $login) && ($loginResult[strUserPass] == $pass) ) {
      
      $_SESSION[flagLoggedIn] = TRUE;
      header ("Location: default/index.php");
			
    } else {
			
      echo 'There was an error, please try again.<br>';
      echo '<a href="index.php">login</a>';
			
    }
  }
?>

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/****/index.php:15) in /home/****/index.php on line 55
I'm not interested in the multitude of security flaws at this point, I just to see what it will take to get the header function to redirect on a successful login.

Again, Thanks!
Last edited by hurdy gurdy on Thu Oct 07, 2004 6:06 pm, edited 2 times in total.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

there should be no output before hedaer()..
or
use ob_start() and ob_end_flush();
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

mudkicker wrote:there should be no output before hedaer()..
or
use ob_start() and ob_end_flush();
I dont understand what you mean by not having any output before the header(). Do you mean that header() should be at the beginning of a script?

I did this quicky to see if that was the case and lo-and-behold I got the error again.

attached

Code: Select all

<? header ("Location: index.php"); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>blah</title>
</head>

<body>
blah blah blah

</body>
</html>
and the error

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/*****/test.php:3) in /home/****/test.php on line 3
blah blah blah
Can someone clarify please?

Thanks!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

ANY output that will be putput to the browser before the header code

INCLUDING white spaces (which you have in your above code.

html, java, etc. Search this forum, this topic has been discussed over n over n over again

:roll:
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

tim wrote:ANY output that will be putput to the browser before the header code

INCLUDING white spaces (which you have in your above code.

html, java, etc. Search this forum, this topic has been discussed over n over n over again

:roll:
Hey Tim, thanks for being understanding.

I did do a search, but to be honest the millions (exaggeration) of results that had nothing to do with the search parameters I submitted sorta put me off. :wink:

If the case that you present is true, then why does this script work? According to your info it shouldn't.

Code: Select all

<?
*snipped DB connection stuff*

if ($strUserName != "") {

    $qryResult = mysql_query ("SELECT strPassword, intUserID, intTotalLogin FROM TableUsers
      WHERE strUserName='$_POST[strUserName]' or strEmail='$_POST[strUserName]'");

    $arrResult  = mysql_fetch_array ($qryResult);

    if ($arrResult [0] == $_POST[strPassword]) {

      // store last login
      session_start ();
      $_SESSION[flagLoggedIn] = TRUE;

      $_SESSION[intUserID] = $arrResult [1];
      $intTotalLogin = $arrResult [2] + 1;

      $dateNow = date ("Y-m-d h:i:s");

      // update login info
      $qryResult = mysql_query ("UPDATE TableUsers SET dateLastLogin = '$dateNow',
        intTotalLogin = $intTotalLogin WHERE intUserID = $_SESSION[intUserID]");

      header ("Location: default/index.php");
    } else {
      $_SESSION[flagLoggedIn] = FALSE;
      $_SESSION[strError] = "Invalid username or password.<br>Please try again.";
    }

  }

?>

<html>

<head>
  <title>.:login:.</title>
  <link rel="stylesheet" type="text/css" href="_templates/stylesheet.css" />
  <META NAME="ROBOTS" CONTENT="NOINDEX,NOFOLLOW">
</head>
<body bgcolor="white" onLoad="document.formLogin.strUserName.focus();">

<form method="post" action="login.php" name="formLogin">

<table width="100%" height="100%">
<tr><td valign=middle align=center>

<?
  if ($_SESSION[strError] != "") {
    print "<font color='red'><b>$_SESSION[strError]</b></font><p>";
    $_SESSION[strError] = "";
  }
?>

<table cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td colspan="3" bgcolor="black" height="1"><img src="_media/1x1trans.gif" width="1" height="1"></td>
  </tr>
  <tr>
    <td bgcolor="black" width="1"><img src="_media/1x1trans.gif" width="1" height="1"></td>
    <td width="200" height="100" valign="middle" align="center">
<table>
  <tr>
    <td>un:</td>
    <td><input type="text" size="20" name="strUserName"></td>
  </tr>
  <tr>
    <td>pw:</td>
    <td><input type="password" size="20" name="strPassword"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="login..."></td>
  </tr>
</table>
    </td>
    <td bgcolor="black" width="1"><img src="_media/1x1trans.gif" width="1" height="1"></td>
  </tr>
  <tr>
    <td colspan="3" bgcolor="black" height="1"><img src="_media/1x1trans.gif" width="1" height="1"></td>
  </tr>
</table>

    </td>
  </tr>
</table>

</form>

</body>
</html>
As you can tell, I am a little confused here. Please be patient, I AM learning.

Thanks again,
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

patrikG wrote:viewtopic.php?t=1157
THANK YOU!

Thats exactly what I needed!
Post Reply