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 */
CodePudding user response:
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.
CodePudding user response:
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();