PHP getting - PHP Notice:  Use of undefined constant

loong
loong used Ask the Experts™
on
Hello,

I am getting this error in our windows server 2008 environment.

PHP Notice:  Use of undefined constant act - assumed 'act' in C:\mydomain.com\admin\action.php on line 4
PHP Notice:  Undefined index:  act in C:\mydomain.com\admin\action.php on line 4

The script is as per attached. Anyone can help?
action.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
How are you defining $r?  Is act defined as a constant in your code?  Otherwise if act is an index of an array, and not a constant, your code should read:
 
$act = $r['act'];

Open in new window

Author

Commented:
Well I am uncertain as this files have been move over from one site to another. Before it was move, the site is running. After moving to our new environment, that's what we get. The only difference we could see is that the previous PHP version was 5.25. and now it is 5.2.17.
The problem with code snippets is that they don't allow you to see the whole picture.  Do you know whether $r is defined in the inc/inc.php file?  Is there any code above the snippet you showed me?  It is kind of strange because two lines below the problem you redefine $r.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Try placing this code on line 3 and tell me if you see anything:

foreach($r as $key => $value) {
	echo "<script language='javascript'>alert('key = " . $key . ", value = " . $value . "');</script>";
}

Open in new window

Author

Commented:
Under inc/inc.php

<?
session_start();
require_once("inc/connect.php");
require_once("inc/function.php");

$r=$_REQUEST;

?>

Author

Commented:
The code I had attach as action.txt on my first mail

Author

Commented:
Putting the code still the same error
PHP Notice:  Use of undefined constant act - assumed 'act' in C:\mydomain.com\admin\action.php on line 7
PHP Notice:  Undefined index:  act in C:\mydomain.com\admin\action.php on line 7

only changes are the line since I had place  the line.

The file that browsing and giving error is action.php which it should as per my previous attach action.txt. Something like that:-

<?php
require_once("inc/inc.php");
foreach($r as $key => $value) {
      echo "<script language='javascript'>alert('key = " . $key . ", value = " . $value . "');</script>";
}

$act = $r[act];
$time = time();
$r=$_REQUEST;

switch ($act)
{

Sheesh, you have a bunch of nested files above that code.  $r could be anywhere.

I see you placed my code, but did the alerts pop up when you ran it?  If so, what were the values of $r?

Author

Commented:
no pops out. exactly similar error with out without your codes. :(

Actually still weird as why previously it was working on another server but not this time...hmm
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I looked at the code that was posted and the first part is simply out of order.  This part:

$act = $r[act];
$time = time();
$r=$_REQUEST;

should be:

$time = time();
$r=$_REQUEST;
$act = $r['act'];

And there isn't a PHP function called 'redirect'.  It should be:

header("Location: pgh.php") ;

http://us.php.net/manual/en/function.header.php
No popups means that $r is not an array.  Are you sure you guys made no changes to the code?

Author

Commented:
Still getting this error this time at line 6 which is $act = $r['act'];

PHP Notice:  Undefined index:  act in C:\mydomain.com\admin\action.php on line 6

Author

Commented:
Yeah no changes as we just pulled the code and place it in the new server
Wait, $_REQUEST comes as a result of a form submission.  Where is your form?

Author

Commented:
Its actually a login page

Maybe u try this -

http://124.150.143.162/admin/loginnew.php

putting in dummy login id and password and u will see the error
OK, I see that in your form there is no way to post an act index.  That could be your first problem.

Did you change the input form at all in your move?

Author

Commented:
I had just bring over the latest file again to the new server. Still the same. Actual domain which yet to point is http://www.palaceofthegoldenhorses.com.my/admin/loginnew.php

We set that ip is to test the whole application before pointing.
None of that affects the fact that the variable which is causing the error has not been defined in any of your code.

I'm sorry to say, but if your code was working before, there is no way that the variable which was supplied to your code is suddenly not there unless someone got in there and changed it.

It might also be that when you transferred the files over, the files might not have been the latest version, or you mixed up local files in the transfer, but whatever the reason, the code you have now is not the same as the code you had when it was working.

Author

Commented:
Its ok we found the problem was due  to reporting error is not off..
I'm sorry, but turning error reporting off will not solve your problem, simply get rid of the annoyance of the warning message, but not resolve the error you have in your coding.

If you are saying that error reporting was off on your original site it may be that this code never worked.

Author

Commented:
Yeah could be actually...Hmmmm
Most Valuable Expert 2011
Top Expert 2016
Commented:
Turning off error_reporting is like putting a piece of black electrical tape over the warning lights on your dashboard.  Something is wrong, but you don't know what and something will break but you don't know when.

A construct like this: $act = $r[act]; tells PHP to try to find the defined constant named act.  When PHP cannot find that, it issues the Notice (note that a Notice is not an Error or a Warning, it is just a Notice).  After issuing the Notice, PHP tries to evaluate the statement as if you had written it this way, with quotes around act: $act = $r['act'];.  This is the correct notation when you want to use a non-variable name as the index of an associative array.

In the default settings for PHP the error reporting level is set to suppress Notices.  As a result, a lot of programmers think, "it just works that way."  But suppressing the Notice creates a work environment that will let you spend days looking for a typographical error.  Accidentally using an undefined variable?  With Notice suppressed, you will not get any help finding this from PHP .  So that is why I turn error_reporting(E_ALL) on 100% of the time, and write my code so that no Notices appear.  It's easier than dealing with silent failures.

Best of luck with the project, ~Ray

Author

Commented:
customer ok to turn off notice
Most Valuable Expert 2011
Top Expert 2016

Commented:
To anyone reading this thread... Do not do what loong is doing here -- it is technically incompetent and potentially dangerous.  

You don't want to be that guy!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial