Link to home
Start Free TrialLog in
Avatar of isas_n
isas_n

asked on

PHP Warning: Use of undefined constant

How to avoid Warning logs for an undeclared variables in PHP?

I have used many variables as,

$_SESSION[dbConnectInfo][host] = "localhost";
$_SESSION[dbConnectInfo][username] = "root";
$_SESSION[sitePD] = $ROOT_PATH;

and so on.

Errors are as follows,
`````````````````
[Wed Sep 15 04:42:23 2004] [error] PHP Warning:  Use of undefined constant dbConnectInfo - assumed 'dbConnectInfo' in /../../../php/master.php on line 17

How to avoid these error logs. PLease give solutions for this to avoid. But I do not want to supress warning logs.
Avatar of ldbkutty
ldbkutty
Flag of India image

>> PHP Warning:  Use of undefined constant dbConnectInfo - assumed 'dbConnectInfo'

The error is here: $_SESSION[dbConnectInfo][host]

dbConnectInfo and host not variables because there is no $ symbol. They are not strings because there is no ' or " enclosed.

It should be "host" and "dbConnectInfo".

But, why do you want to store the host and username in SESSION? Are you trying to keep it in session and use it in the required pages. If so, SESSION is not really the best way. Just do the DB Connection in a PHP and include it in the required pages.
even then, why 2-D for sessions !? this should be enough:

$_SESSION["dbConnectInfo_host"] = "localhost";
$_SESSION["dbConnectInfo_username"] = "root";
$_SESSION["sitePD"] = $ROOT_PATH;
Avatar of isas_n
isas_n

ASKER

Hi, ldbkutty

I'm trying to make changes in the code. I will let you know after some time.
`````````````````````````````````````````````````
For example,

$_SESSION[webImgPath] = $_SESSION[siteURL]."/userdata/uploadedimages";

In Most of the pages we are using, the variable,
$_SESSION[webImgPath].

So we can store it in a session, so that it will be used wherever required.

Also we are using only sessions in our program,
eg. Storing field values. That field value will be used in different forms.

Is there any alternate way for this?

>> Also we are using only sessions in our program, eg. Storing field values
>> Is there any alternate way for this?

If you just want to pass the form-fields to the next page, POST method is enough to retrieve in the next page.
POST carries the field values to the next page, SESSIONS carry all over the pages.

>> $_SESSION[webImgPath]

This is not the right format for SESSIONS. you should have it as: $_SESSION['webImgPath'] or $_SESSION["webImgPath"].

With $_SESSION[webImgPath], If your warnings are suppressed in your php.ini(which is not secure), you wont get any warnings in your output page. Otherwise it will show errors like you got.

So the right format is:

$_SESSION["webImgPath"] = $_SESSION["siteURL"]."/userdata/uploadedimages";

I hope you understand.
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isas_n

ASKER

hello