I need some help. I'm trying to use the header() at the top of my code. If the register button is pressed it should send me to a registration.php page. Also if they press the login button i want to check their name and password against a database and if that comes back Ok i want to direct them to the index.php page. The problem is that the 2nd header() I use is at the bottom of my code and cant really be worked around... is there another way of changing pages using PHP that im not aware of? If not whats the workaround i can use? Thanks in advance!

Almost forgot to mention im gettin this error. Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\ebaylogin.php:6) in D:\xampp\htdocs\ebaylogin.php on line 81

When i goto line 5 it takes me to the ending brackets in this...
if($_POST['register'])
{
header("location:ebayregistration.php");
} <---- right there

    Jgorge wrote:

    The problem is that the 2nd header() I use is at the bottom of my code and cant really be worked around

    Why? Why can't you move the part of the code that does some processing to determine if a header() needs to be sent before the HTML?

      I'll give a better description. I have a login screen with 2 textboxes for username and password, a login button and a register button. At the top of all that i have a <a href> to another page. If the register button is hit, it does this
      header(location:register.php). If the login button is hit it processes the form with the username and password text boxes and checks a database to see if they are indeed in that database. If they are it should goto this
      header(location:index.html). The problem is i keep gettin that error saying theres already another header in use more or less and its the one from above. I'm not sure how to get around this. Thanks for any help you can give!🙂

        Ok heres whats going on. I have a form called login.php. It has 2 text fields for username and password and 2 buttons login and register. I also have a <a ref> at the top that will take me to the homepage. When the register button is pressed i simply do this header(location:register.php). When the login button is pressed i need to process the form and check to see if the username and password do indeed exist and if they do i need to do header(location:index.html). The problem is that the header for reigster.php is way at the top and i dont see a way of having the other header not being at the end because it has to process all the information and then switch pages. Maybe im going about this the wrong way. If you can help i'd be very appreciative. Thanks!

        ****Sorry double post, about a hour delay between my postings *****

          I would NOT use header() redirect for this, as you have a whole lot of problems with variable handling. Rather I would have both routines (register & login) as empty routines, so not header, footer, banner, menu etcetc and include them when required:

          if($login)
            {
            require(_login_);;
            }
          else
            {
            require(_register_);
            }
          

          If you need to use headers, just make sure you test for what was pressed as the first thing in your script, do the redirect. You cannot output ANYTHING before headers are called. They need to be the first part of your output.

            <?
            include_once "ebayfunctions.php";
            if($_POST['register'])
            {
            header("location:ebayregistration.php");
            }
            connect();
            echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"";
            echo "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
            echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"eng\" xml:lang=\"en\">";
            echo "<head>";
            echo "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />";
            echo "<title>Register</title>";
            echo "<link rel=\"stylesheet\" href=\"../hornet.css\" type=\"text/css\" />";
            echo "</head>";
            echo "<p>";
            echo "<em><a href=\"../index.html\">HornetTrade.com</a></em>";
            echo "</p>";

            if(!$POST['login'] && !$POST['register'] )
            {
            echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
            echo "<form method=\"post\" action=\"ebaylogin.php\">\n";
            echo "<tr><td colspan=\"2\" align=\"center\">Login</td></tr>\n";
            echo "<tr><td>UserName</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
            echo "<tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>\n";
            echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"login\" value=\"Login\">\t\t\t<input type=\"submit\" name=\"register\" value=\"Register\"></td></tr>\n";
            echo "</form></table>\n";
            }else
            {
            $username = protect($POST['username']);
            $password = protect($
            POST['password']);
            $errors = array();
            if(!$username)
            {
            $errors[] = "Username is not defined!";
            }

            if($username)
            {
            	$range = range(1,32);
            	if(!in_array(strlen($username), $range))
            	{
            		$errors[] = "Username must be between 1 and 32 characters long";
            	}
            }
            
            if(!$password)
            {
            	$errors[] = "Password is not defined!";
            }
            
            
            
            if($password)
            {
            	$range2 = range(1,50);
            	if(!in_array(strlen($password), $range2))
            	{
            		$errors[] = "Password must be between 1 and 50 characters long";
            	}
            
            
            }
            
            
            
            if(count($errors) > 0)
            {
            	foreach($errors AS $error)
            	{
            		echo $error . "<br>\n";
            	}
            
            }else
            {
            	if($username && $password)
            	{
            		$sql = "SELECT * FROM `user` WHERE `Username`='$username' AND `Password`='$password'";
            		$res = mysql_query($sql) or die(mysql_error());
            
            		if(mysql_num_rows($res) > 0)
            		{
            			header("location:index.php");
            		}else
            		{
            			echo "User Name or Password is incorrect";
            		}
            
            	}
            
            
            }

            }
            ?>

            This is my code. I have it set so that if neither buttons are pressed then show the HTML for the login page. I dont see how i can do the header first because it requires some database stuff to decide on which page to goto.

              What is stopping you from moving all that PhP code to the top of the page?

                I thought the header had to be first? I dont think i follow. How should i have it be? Also its giving me a error at the connect(); now.

                  The header() has to be the first output. PhP code can be run before it, as long as no output is generated.

                    Thank you very much leatherback i think i got it working. I'm sure you'll see me again though on these forums! thanks!

                      Write a Reply...