By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

php 5 and uninitialized variables

P: n/a
i've setup a include page that's responsible for building the basic layout
of my web pages (header, menus, etc.). each page includes this
"sysheader.php".

the first page i've built with this header is having odd results. its job is
to add users to the system. the variables i expect to be there are: id,
userName, password...stuff like that. after the require_once
"sysheader.php", i've put an "exit;" statement. the header shows perfectly.
if i try and access or set one of the expected variables, only part of the
header shows...crapping out around the time it is building the company
logo - so that i see "<img src="http://www" and nothing else.

i've tried to set my own variable order and initialze the variables myself
like this:

$id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);

but that doesn't work either. i've been running similarly constructed pages
successfully under php < 5...what am i missing now that i'm moving to php 5?

tia,

steve

Jul 17 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
steve wrote:
i've setup a include page that's responsible for building the basic
layout of my web pages (header, menus, etc.). each page includes this
"sysheader.php".

the first page i've built with this header is having odd results. its
job is to add users to the system. the variables i expect to be there
are: id, userName, password...stuff like that. after the require_once
"sysheader.php", i've put an "exit;" statement. the header shows
perfectly. if i try and access or set one of the expected variables,
only part of the header shows...crapping out around the time it is
building the company logo - so that i see "<img src="http://www" and
nothing else.

i've tried to set my own variable order and initialze the variables
myself like this:

$id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);


REQUEST[ 'id' ] if my memory serves me correctly has either POST or GET
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #2

P: n/a
steve wrote:
i've tried to set my own variable order and initialze the variables
myself like this:

$id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);

but that doesn't work either. i've been running similarly constructed
pages successfully under php < 5...what am i missing now that i'm
moving to php 5?


You could also do something like:

$id = @$_REQUEST['id'];

This will catch the id when it's posted or passed as a GET parameter or
create an empty variable without warnings, whichever is appropriate.

Anyways, I'm almost certain that your code relies on register_globals being
enabled, while with PHP 5 it's disabled by default as it should.

Enable the register_globals directive in your php.ini file and check if the
code works. When it does, you will have to convert all code that relies on
register_globals, which also includes the session system.
JW

Jul 17 '05 #3

P: n/a
| REQUEST[ 'id' ] if my memory serves me correctly has either POST or GET

thank for the info...didn't fix the problem though. any other ideas on how
to handle expected, yet uninitialized, variables? can i change something in
the php.ini file?
Jul 17 '05 #4

P: n/a
steve wrote:
thank for the info...didn't fix the problem though. any other ideas
on how to handle expected, yet uninitialized, variables? can i change
something in the php.ini file?


Use isset() to check if they exist or prepend the @ sign to surpress error
messages. Don't mess with the error_reporting settings, because surpressing
notices and warnings at this level will only cause errors to be harder to
find.
JW

Jul 17 '05 #5

P: n/a
| You could also do something like:
|
| $id = @$_REQUEST['id'];
|
| This will catch the id when it's posted or passed as a GET parameter or
| create an empty variable without warnings, whichever is appropriate.
|
| Anyways, I'm almost certain that your code relies on register_globals
being
| enabled, while with PHP 5 it's disabled by default as it should.
|
| Enable the register_globals directive in your php.ini file and check if
the
| code works. When it does, you will have to convert all code that relies on
| register_globals, which also includes the session system.

thanks for the input...it doesn't take care of the problem though. i have
register_globals=Off and did not have it set otherwise in previous versions
of php. enabling that setting in php 5 sends it merrily running on and on
until i have to kill the process manually.

i do get output but the buffer only spews until it hits the line of code
where a variable is utilized (set/get) but not initialized.

any further input is greatly apprecieated.
Jul 17 '05 #6

P: n/a
| Use isset() to check if they exist or prepend the @ sign to surpress error
| messages. Don't mess with the error_reporting settings, because
surpressing
| notices and warnings at this level will only cause errors to be harder to
| find.

i have tried using isset() and the @ sign previously...but to no avail.
should i just turn on logging and see what php reports?
Jul 17 '05 #7

P: n/a
steve wrote:
thanks for the input...it doesn't take care of the problem though. i
have register_globals=Off and did not have it set otherwise in
previous versions of php. enabling that setting in php 5 sends it
merrily running on and on until i have to kill the process manually.

i do get output but the buffer only spews until it hits the line of
code where a variable is utilized (set/get) but not initialized.


Okay, then it's time to try changing the error_reporting level. This will
only be a test, ensure to restore it to its original level when done
testing.

Go to the ini file and check that the error_reporting directive looks as
follows:

error_reporting = E_ALL & ~E_NOTICE

Then check if the scripts work now.
JW

Jul 17 '05 #8

P: n/a
steve wrote:
i have tried using isset() and the @ sign previously...but to no
avail. should i just turn on logging and see what php reports?


Ah, so you don't see any error output, but the script just stops? Yes, then
you definitely need to enable logging.

You can enable the display_errors directive in the ini file for easy
debugging.
JW

Jul 17 '05 #9

P: n/a
| Ah, so you don't see any error output, but the script just stops? Yes,
then
| you definitely need to enable logging.
|
| You can enable the display_errors directive in the ini file for easy
| debugging.

right...but this is also part of my quandry. display_errors is set to On
*and* i don't see any errors in my browser prior to the script crapping out.
Jul 17 '05 #10

P: n/a
| Okay, then it's time to try changing the error_reporting level. This will
| only be a test, ensure to restore it to its original level when done
| testing.
|
| Go to the ini file and check that the error_reporting directive looks as
| follows:
|
| error_reporting = E_ALL & ~E_NOTICE
|
| Then check if the scripts work now.

that's what it was originally set to...i changed it to only show errors. in
combination with that, i've added:

$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;

and cleared my browser's history. there doesn't seem to be a problem with
the above now.

i guess that solve the script-stoppage problem, but i'd like to know why
uninitialized variable useage would cause the problem *and* not even give an
error message of some kind.

thanks for your help.
Jul 17 '05 #11

P: n/a
steve wrote:
right...but this is also part of my quandry. display_errors is set to
On *and* i don't see any errors in my browser prior to the script
crapping out.


Can you post some of the code which then is encountered?
JW


Jul 17 '05 #12

P: n/a
sure:

here's the "menu" page where it is intended to give a web page a
description, whether is requires user access rights, it's url is defined,
etc...

<?
$pageTitle = "User Menus";
require_once "../inc/head.inc.php";

$sql = "";
// echo $_REQUEST['id']; // script craps out here if uncommented
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is ok.
exit;
?>

here's the "head.inc.php" script (i can include all dependent files
(site.cfg.php, base.css.php, etc. if you are trying to fully recreate the
problem):

<?
include_once "../htdocs/site.cfg.php";
$pageAccess = "";
$userId = -1;
if (isset($userName) && isset($userPassword))
{
$sql = "
SELECT Id,
FirstName,
LastName,
(
SELECT COUNT(*) IsValid
FROM people
WHERE UserName = '$userName'
AND Password = '$userPassword'
) IsValid
FROM people
WHERE UserName = '$userName'
AND Password = '$userPassword'
";
$records = odbc_exec($db, $sql);
$validUser = odbc_result($records, "IsValid") > 0;
$userFullName = odbc_result($records, "FirstName") . " " .
odbc_result($records, "LastName");
$userId = odbc_result($records, "Id");
}
if ($validUser)
{
if ($userMenu == "")
{
$sql = "
SELECT w.Category,
w.Description,
w.Url
FROM accessList a,
webPages w
WHERE w.Id = a.Url
AND a.person = $userId
ORDER BY w.Category,
w.Description,
ViewOrder
";
$records = odbc_exec($db, $sql);
$currentCat = "";
$userMenu = "";
$i = 0;
while(odbc_fetch_row($records))
{
$category = odbc_result($records, "Category");
$description = odbc_result($records, "Description");
$url = odbc_result($records, "Url");
if ($category != $currentCat)
{
if ($category != ""){ $userMenu .= "</div>\r\n"; }
$userMenu .= "<div class=\"items\"
onclick=\"document.getElementById('_$i').style.dis play =
(getElementById('_$i').style.display == 'none' ? 'block' : 'none');\">";
$userMenu .= "<a alt=\"$category\" href=\"\" onclick=\"return
false;\">$category</a>";
$userMenu .= "</div>\r\n<div id=\"_$i\" style=\"display:'none';
margin-left:'15px';\">\r\n";
$i++;
}
$userMenu .= "<span style=\"padding:'2px';\"><a href=\"$url\"
alt=\"$description\">$description</a></span><b><b></b></b><br>\r\n";
}
if ($i != 0){ $userMenu .= "</div>\r\n"; }
setcookie("userMenu", $userMenu);
}
}
echo $sessionHeader;
?>
<!------------ marker for top ------>
<a name="#top"></a>
<!------------ top menu ------------>
<div class="menuTop">
<div class="title">
<b><big><?= $siteTitle ?></big></b>
</div>
<?
if ($validUser)
{
?>
<span class="title" style="font-size:'<?= $sessionFonts["NORMAL"]
?>';">
<?= $userFullName ?>
</span>
<br>
<?
}
?>
<span class="items">
<?
// generate menu items

$stdMenu = array(
"Home" => $siteUrl
);
foreach($stdMenu as $description => $url)
{
echo " <a href=\"$url\" alt=\"$description\"><span
class=\"item\">$description</span></a>\r\n";
}
if ($validUser)
{
echo " <a href=\"?logout\" alt=\"Log Out\"><span class=\"item\">Log
Out</span></a>\r\n";
}
?>
</span>
</div>
<!------------ left menu ----------->
<span class="menuLeft">
<br>
<div class="logo">
<img src="<?= $siteCompanyLogo ?>" alt="Company Logo">
</div>
<div style="font-size:'<?= $sessionFonts["NORMAL"] ?>'; margin:'5px';
width:'90%'">
<b><i><?= $siteDescription ?></i></b><br>&nbsp;
</div>
<div class="items">
<?
// generate menu items

$stdMenu = array(
"Home" => $siteUrl
);
foreach($stdMenu as $description => $url)
{
echo " <a href=\"$url\"
alt=\"$description\">$description</a><br>\r\n";
}
?>
</div>
<?
if ($userMenu != "")
{
echo $userMenu;
}
?>
<br>
<form method="get" target=_blank
action="http://www.google.com/custom">
<div class="row">
<span class="label" style="background-color:'#000067'; border:'1px
solid white'; width:'100%';">
<span class="label" style="color:'white'; font-size:'<?=
$sessionFonts["LARGE"] ?>'; margin:'5px';">
<b>Search<b>
</span>
</span>
<a href="http://www.google.com/search" target=_blank></a>
<br>
<br>
<input class="value" type="text" name="q" maxlength="255"
autocomplete="off" style="font-size:'<?= $sessionFonts["NORMAL"] ?>';
margin-right:'2px'; width:'100%';">
<br>
<br>
<span class="label"><?= $siteHost ?></span>
<input style="width:'10px';" type="radio" name="sitesearch"
value="<?= $siteHost ?>" checked>
<span class="label">The Web</span>
<input style="width:'10px';" type="radio" name="sitesearch"
value="">
<br>
<br>
<span class="label" style="text-align:'center';
width:'100%'"><input type="submit" name="sa" value="Search"></span>
</div>
<br>
<a href="http://www.google.com/search" target=_blank>
<img src="http://www.google.com/logos/Logo_25wht.gif" border="0px"
alt="Google" name="Google"></a>
<br>
<span class="label" style="text-align:'left';
width:'100%';"><i>[Search Powered By Google]</i></span>
<input type="hidden" name="cof" value="LW:170;L:<?= $siteCompanyLogo
?>;LH:36;AH:left;AWFID:b16640e8f2fd59e3;">
<input type="hidden" name="domains" value="<?= $siteHost ?>">
</form>
</span>
<!------------ body text ----------->
<span class="body">
<br>
<div class="pageTitle">
<img src="<?= $siteLogo ?>" style="float:'right';" alt="<?=
$pageTitle ?>">
<br clear="all">
<span style="margin:'20px';"><big><b><?= $pageTitle
?></b></big></span>
<hr>
</div>
Jul 17 '05 #13

P: n/a
steve wrote:
<?
$pageTitle = "User Menus";
require_once "../inc/head.inc.php";

$sql = "";
// echo $_REQUEST['id']; // script craps out here if uncommented
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is
ok. exit;


This probably doesn't break the script, try the following:

<?
echo $_REQUEST['newvar'];
echo "hello";
?>

This will probably print the expected "hello". When it does, the following
question would be, where is $id used in the code? When it's used in a
database query and you do something like "WHERE id=$id" while $id is empty,
the error would originate from the ODBC driver, because the query will be
invalid.

However, when you don't see any output and nothing is recorded in the log
files, it's time to check the installation of PHP. Perhaps something went
wrong during the compilation of PHP.
JW
Jul 17 '05 #14

P: n/a

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl...
| steve wrote:
| > <?
| > $pageTitle = "User Menus";
| > require_once "../inc/head.inc.php";
| >
| > $sql = "";
| > // echo $_REQUEST['id']; // script craps out here if uncommented
| > $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is
| > ok. exit;
| >
|
| This probably doesn't break the script, try the following:
|
| <?
| echo $_REQUEST['newvar'];
| echo "hello";
| ?>
|
| This will probably print the expected "hello". When it does, the following
| question would be, where is $id used in the code? When it's used in a
| database query and you do something like "WHERE id=$id" while $id is
empty,
| the error would originate from the ODBC driver, because the query will be
| invalid.
|
| However, when you don't see any output and nothing is recorded in the log
| files, it's time to check the installation of PHP. Perhaps something went
| wrong during the compilation of PHP.

i'm re-installing now as a matter of fact. i'll let you know how things go.

thanks again for all your help.
Jul 17 '05 #15

P: n/a
> <?
$pageTitle = "User Menus";
require_once "../inc/head.inc.php";


It is probably the above code giving you problems? Try this... Change the
following code:

require_once "../inc/head.inc.php";

to:

if (!include_once("../inc/head.inc.php")) exit('Unable to open
"../inc/head.inc.php"');

If require_once has a problem your script will just exit quietly.

____________________________________
Wil Moore III, MCP | Integrations Specialist
Jul 17 '05 #16

P: n/a
| It is probably the above code giving you problems? Try this... Change the
| following code:
|
| require_once "../inc/head.inc.php";
|
| to:
|
| if (!include_once("../inc/head.inc.php")) exit('Unable to open
| "../inc/head.inc.php"');
|
| If require_once has a problem your script will just exit quietly.
thanks for your thoughts, however i know that the required page is being
included. it only displays incorrectly when i access an uninitialized
variable.

i've removed php 5 and reverted to php 4.3. the problem is gone now. i'll
probably fully develop this small site, uninstall completely 4.3, then
install 5 and see what blows up.

thanks again,

steve
Jul 17 '05 #17

P: n/a
"steve" <a@b.com> wrote in message news:es*****************@fe03.lga...
that's what it was originally set to...i changed it to only show errors. in combination with that, i've added:

$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;

and cleared my browser's history. there doesn't seem to be a problem with
the above now.

i guess that solve the script-stoppage problem, but i'd like to know why
uninitialized variable useage would cause the problem *and* not even give an error message of some kind.


Quite possibly a bug in PHP 5. Sounds like one that causes the server to
sigfault, since PHP didn't even manage to flush the output buffer.
Jul 17 '05 #18

P: n/a
| Quite possibly a bug in PHP 5. Sounds like one that causes the server to
| sigfault, since PHP didn't even manage to flush the output buffer.

i was fearful of that and, all indications are that it is so since reverting
to php 4.3 and running the same code gives no errors and produces exactly
the expected results.
Jul 17 '05 #19

P: n/a
In comp.lang.php steve <a@b.com> wrote:
| Quite possibly a bug in PHP 5. Sounds like one that causes the server to
| sigfault, since PHP didn't even manage to flush the output buffer.

i was fearful of that and, all indications are that it is so since reverting
to php 4.3 and running the same code gives no errors and produces exactly
the expected results.


At the same level of error_reporting?

Jul 17 '05 #20

P: n/a
Hell off topic with them.. but you might want to check
htmlspecialchars() on that script there.. sql injections visible at
first glance..

but anyways, you were on the right track with isset, i suggest just
making a simple function?
that is, if you are trying to assign a post/get variable to a php
variable... you could do somthign like this..

function setvar($var){
if(isset($_POST[$var])){
return $_POST[$var];
}
if(isset($_GET[$var])){
return $_GET[$var];
}
return "";
}

then just use
$p = setvar("p");
and p will be either the get, post, or "".

but the method you are using is doing one isset, with my local apache
it brings up errors like you have when i have unassigned variables, so
the easiest and safest thing to do is set them to null, then use the
isset..

$p = "";
if(isset($_GET['p'])) $p = $_GET['p'];

that will cause p to only be set with the get value if there is one..
(duh)

Jul 17 '05 #21

P: n/a

"Daniel Tryba" <sp**@tryba.invalid> wrote in message
news:41**********************@news.xs4all.nl...
| In comp.lang.php steve <a@b.com> wrote:
| > | Quite possibly a bug in PHP 5. Sounds like one that causes the server
to
| > | sigfault, since PHP didn't even manage to flush the output buffer.
| >
| > i was fearful of that and, all indications are that it is so since
reverting
| > to php 4.3 and running the same code gives no errors and produces
exactly
| > the expected results.
|
| At the same level of error_reporting?

hard to say "same" here, as i tried all different combinations of error
reporting on php 5. it simply stopped spewing output when it hit an
uninitialized variable. with php 4.3, i have e_all & ~ e_notice...which is
the default i believe.

i don't know if it made a difference in installation, but i did not
uninstall php 4.3 prior to the php 5 install...which did not require a
re-boot once complete (and i hadn't rebooted manually either). when the
problem showed itself and i could not resolve it, i uninstalled php 4.3. the
problem persisted. i uninstalled php 5 (*did* require a reboot) then
reinstalled php 4.3. may just have had a dll from 4.3 in memory until the
reboot...or something like that.

when i retry php 5, i'll take off php 4.3, reboot, then install 5 and see
what happens with the app then.

thanks for everyone's help.
Jul 17 '05 #22

P: n/a
| Hell off topic with them.. but you might want to check
| htmlspecialchars() on that script there.. sql injections visible at
| first glance..

yeah, there are a couple of things i'll go back and fix after i get the bulk
of the script written...i need to add data-prep (validation, error
reporting, etc.) which will be responsible for prep-ing it for use with sql
among other things.

mainly just working out connectivity, basic sql, and page layout(s) now.

| but anyways, you were on the right track with isset, i suggest just
| making a simple function?

i actually changed it to use $_request (which uses the php.ini settings for
variable assignment order):

$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;

this accomplishes the same thing as the example you posted and always sets
the var.

thanks for your thoughts and suggestions!

cheers,

steve
Jul 17 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.