0

I have a problem when running a php script, calling a second script using header().

The main script (a.php) calls, while running, a second script (b.php).

When using this form all goes as expected (ActionA and ActionB are performed):

https://example.com/a.php

When using this other form, the expected behavior (ActionAX and ActionBX) is not taking place. Instead ActionAX and ActionB are performed:

https://example.com/a.php?V=X

In other words b.php does not get the V=X bit of information.

The file a.php looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="content-type" CONTENT="text/html; CHARSET=UTF-8">
<TITLE>MyWebApp</TITLE>
<STYLE>
INPUT[type='Submit'] {font-size: 19px;}
</STYLE>
</HEAD>
<BODY bgcolor="#A1E3D2">

<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionAX();
else  performActionA();
}/* End of getVarStr */
.....
getVarStr();
.....
if ($_GET['V'] == 'X') header("Location: ./b.php?V=X");
header("Location: ./b.php");
.....
?>
</BODY>
</HTML>

The file b.php looks like this:

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,  initial-scale=1">
        <title>Extra Page</title>
        <STYLE>
                INPUT[type='Submit'] {font-size: 19px;}
        </STYLE>
</head>
<body>

<?php
session_start();
.....
function getVarStr()
{/* Beginning of getVarStr */
if ($_GET['V'] == 'X') performActionBX();
else  performActionB();
}/* End of getVarStr */
.....
getVarStr();
.....
?>
</BODY>
</HTML>

Can anyone point out a mistake I am making ?

I have also tried to handle the issue using session variables, but I hit exactly the same problem.

I this case I change the getVarStr() inside b.php to:

function getVarStr()
{/* Beginning of getVarStr */
if ($_SESSION['V'] == 'X') performActionBX();
else  performActionB();
}/* End of getVarStr */

2 Answers 2

1

If by "php script called with header()" you're refering to the header("Location.... line in your code, then you have quite an incorrect picture of what really goes on here.

The location header is simply a string added to the http response; header() does more or less the same as echo, except it is intended to happen earlier on, prior to any of the http replys' body being written (more on that later).


Functionally, what the Location header really does, is simply let the users web browser know, that you would like them to visit another page/url instead, which (under most circumstances) it will comply with. It is not unlike sending a

   <script> window.location = 'https://....';</script>

within your html reply; except the http header doesn't rely on java-script being enabled.


Now, the actual problem is the way you've structured your code. You cannot have written any output, before you attempt to add a header to the http reply. Doing so prevents PHP from being allowed/able to add that header.

Both your header() and session_start() (which indirectly also uses header() to place a cookie), are located too far down in your code to function properly.

Infact, in both your a.php and b.php files, the very first lines (<!DOCTYPE...) already produce output, which is a big no-no.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for your comments. Does that mean I need to put this header("Location: ....."); line at the very beginning of my source code. I must admit I have almost never used this header("Location: .....") thing and don't understand very much how it works.
I'm not sure what you're asking. If you're asking if you should literally add that ? no. That line was just an abbreviation of the line(s) you're already using (only in the wrong place).
OK. Then my question is: Where is the right place?
The easiest fixed is to stop inlining html in your php files. That basically means: anything outside* of the <?php ?> blocks, is bad. Right at the top; from <!DOCTYPE html> to <body>; that should all be taken away and placed somewhere else.
You cannot be producing any content prior to having performed your header/session functions. So besides the inlined html. that also means any echo/print/printf/var_dump etc function cannot come before the header & session_start.
|
1

always put php source above html specially if they modify cookies or session to prevent encountering "Warning: Cannot Modify Header Information – Header Already Sent" issue also you might as well put this anywhere so you know the data you applying are actually being applied


echo "SESSION NAME: " . session_name();
echo "<br>";
echo "SESSION ID: " . session_id();
echo "<br>";
echo "TIME: " . time();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.