Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

PHP getting - PHP Notice: Use of undefined constant

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
0
loong
Asked:
loong
  • 12
  • 9
  • 2
  • +1
2 Solutions
 
OmniUnlimitedCommented:
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

0
 
loongAuthor 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.
0
 
OmniUnlimitedCommented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
OmniUnlimitedCommented:
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

0
 
loongAuthor Commented:
Under inc/inc.php

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

$r=$_REQUEST;

?>

0
 
loongAuthor Commented:
The code I had attach as action.txt on my first mail
0
 
loongAuthor 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)
{

0
 
OmniUnlimitedCommented:
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?
0
 
loongAuthor 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
0
 
Dave BaldwinFixer of ProblemsCommented:
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
0
 
OmniUnlimitedCommented:
No popups means that $r is not an array.  Are you sure you guys made no changes to the code?
0
 
loongAuthor 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
0
 
loongAuthor Commented:
Yeah no changes as we just pulled the code and place it in the new server
0
 
OmniUnlimitedCommented:
Wait, $_REQUEST comes as a result of a form submission.  Where is your form?
0
 
loongAuthor 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
0
 
OmniUnlimitedCommented:
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?
0
 
loongAuthor 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.
0
 
OmniUnlimitedCommented:
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.
0
 
loongAuthor Commented:
Its ok we found the problem was due  to reporting error is not off..
0
 
OmniUnlimitedCommented:
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.
0
 
loongAuthor Commented:
Yeah could be actually...Hmmmm
0
 
Ray PaseurCommented:
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
0
 
loongAuthor Commented:
customer ok to turn off notice
0
 
Ray PaseurCommented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 12
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now