Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

header() sometimes works; sometimes not.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

header() sometimes works; sometimes not.

Postby curtisdehaven » Sun Jul 13, 2014 11:51 am

Consider the code below - it presents a page with a username and password. Upon pressing a submit button, the php code on the server redirects to some other page via a call to header().

The problem is that when that other page is passed in as a query string variable, it doesn't work. When it's hard coded it does.

The initial page is accessed via....

There's a conditional in the code - when the initial page is displayed, it shows "true" at the top of the page.

When the submit button is pressed, we see This Page Can't Be Displayed.

However, if we un-comment the line showing "$ReturnURL = $ReturnURL2;", it works fine - we're taken to the other page.

Why the difference?

Thanks! Curt

Here's the code...
Code: Select all
<html xmlns="">



      $Submit = GetPost('Submit');
      $Username = GetPost('Username');
      $Password = GetPost('Password');


      if ($ReturnURL == $ReturnURL2)
        echo "true";
        echo "false";
//      $ReturnURL = $ReturnURL2; 

      if ($Submit != "")
        header("Location: $ReturnURL");
      function GetPost($var)
        if (isset($_POST[$var]))
          return $_POST[$var];
          return "";
      function GetQuery($var)
        if (isset($_GET[$var]))
          return $_GET[$var];
          return "";



    <form method="post" action="Logon.php" id="form1">

                <td><input id="Username" name="Username" type="text" value="<?php echo $Username; ?>" /></td>

                <td><input id="Password" name="Password" type="text" /></td>

                <td> </td>
                <td><input id="Submit" name="Submit" type="submit" value="Submit" /></td>


New php-forum User
New php-forum User
Posts: 1
Joined: Sun Jul 13, 2014 11:37 am

Re: header() sometimes works; sometimes not.

Postby kladrian » Thu Aug 14, 2014 11:13 am

try to move php code outside html code, put it at the very beginning of the file

New php-forum User
New php-forum User
Posts: 12
Joined: Thu Aug 14, 2014 1:13 am

Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.