header() sometimes works; sometimes not.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

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....
___://.../nss/Logon.php?ReturnURL=%2fnss%2fAdminCalendar.php

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="http://www.w3.org/1999/xhtml">

  <head>

    <title></title>

    <?php
 
      $Submit = GetPost('Submit');
     
      $Username = GetPost('Username');
      $Password = GetPost('Password');

      $ReturnURL=GetQuery('ReturnURL');
     
      $ReturnURL2="/nss/AdminCalendar.php";

      if ($ReturnURL == $ReturnURL2)
        echo "true";
      else
        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 "";
      }
 
     ?>


  </head>

  <body>

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

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

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

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

        </table>

    </form>
     
  </body>
 
</html> 

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

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

---
kladrian


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest