Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP getting - PHP Notice:  Use of undefined constant

Posted on 2011-09-05
24
Medium Priority
?
734 Views
Last Modified: 2012-06-27
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
Comment
Question by:loong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 2
  • +1
24 Comments
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36486786
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
 

Author Comment

by:loong
ID: 36486810
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
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36486831
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36486833
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
 

Author Comment

by:loong
ID: 36486880
Under inc/inc.php

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

$r=$_REQUEST;

?>

0
 

Author Comment

by:loong
ID: 36486885
The code I had attach as action.txt on my first mail
0
 

Author Comment

by:loong
ID: 36486890
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
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36486909
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
 

Author Comment

by:loong
ID: 36486943
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 36486971
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
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36486988
No popups means that $r is not an array.  Are you sure you guys made no changes to the code?
0
 

Author Comment

by:loong
ID: 36487025
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
 

Author Comment

by:loong
ID: 36487032
Yeah no changes as we just pulled the code and place it in the new server
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36487039
Wait, $_REQUEST comes as a result of a form submission.  Where is your form?
0
 

Author Comment

by:loong
ID: 36487068
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
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36487106
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
 

Author Comment

by:loong
ID: 36487392
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
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 600 total points
ID: 36487409
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
 

Author Comment

by:loong
ID: 36487435
Its ok we found the problem was due  to reporting error is not off..
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36487471
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
 

Author Comment

by:loong
ID: 36487586
Yeah could be actually...Hmmmm
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 36957277
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
 

Author Closing Comment

by:loong
ID: 37174737
customer ok to turn off notice
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37176179
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question