PHP Register Globals Explained - Headers already Sent - Undefined Variable

by Hiroshi on 19-04-2008

There are some situations where simple code may work properly in case of PHP online or at localhost if register_globals are on. But not necessarily if register_globals are off.
Warning: Undefined Index - Use of Undefined constant selected
Warning: Header already sent
Sometimes messages like above that header already sent or undefined variable or index can appear.
Recommendation: You can use javascript redirect instead of header redirect.
Header Redirect
header("location: somepage.php?action=posted");

Use javascript redirection if header redirect is not working. i.e.

<script language="javascript" type="text/javascript">
<!--
alert("Please Login.");
document.location = "index.php";
-->
</script>

Use @ to avoid warnings

<? session_start();
if(@$_SESSION['usrName']=="") {
?>
And
$getname=@$_REQUEST["name"];

Note how I have used @ which is not necessary to use but working at strict server and with globals off to avoid warnings you can use this.
Register_Globals are by default off. These are for security reasons. Meant to be like it. It is recommended to program with globals off settings and use POST and REQUEST always while passing variables through pages. i.e.

$getact=$_REQUEST["act"];
$_POST["act"]

OR
echo $_POST["act"];
if(isset($_GET['action']) && $_GET['action']=="download")
{ // do something }

  • Share/Save/Bookmark

Stumble it Digg it Bookmark it Tweet it Share at Facebook

Related Posts

Post a Comment

Comment will appear here after approval, Thanks for patience