PHP Notice : Undefined variable & index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

PHP Notice : Undefined variable & index

Post by bouncer »

i'm getting this notices ...

Code: Select all

PHP Notice:  Undefined variable: cet ...
PHP Notice:  Undefined variable: referer ...

Code: Select all

if ($cet) {
	$usacetelem=1;
	session_register("usacetelem");
}

if (strlen($referer) == 0) {
	if (strlen(strstr($HTTP_REFERER,'loja')) > 0) { $referer = "http://".$HTTP_HOST; }
	else { $referer = $HTTP_REFERER; }
	session_register("referer");
}
else {
	if (strlen(strstr($HTTP_REFERER,'loja')) == 0) { $referer = $HTTP_REFERER; }
	session_register("referer");
}
why i'm getting this notices, and how i can eliminate them ?

thanks in advance
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

You have to "create" the variable before you can use it.

At the top of your script, before any data is passed to them...

Code: Select all

var $cet;
var $referer;
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

dhrosti wrote:You have to "create" the variable before you can use it.

At the top of your script, before any data is passed to them...

Code: Select all

var $cet;
var $referer;
i've add those line to my code, and now i'm getting this ...

Code: Select all

PHP Parse error:  syntax error, unexpected T_VAR ...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Can you post your code again, and the entire error message?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

<?
session_start();
require "../lib/config.obj.php";
require "$dir_site/lib/globalserver.obj.php";
require "loja.obj.php";

if ( $cet ) { 
	$usacetelem = 1;
	session_register ( "usacetelem" );
}

if ( strlen ( $referer ) == 0 ) {
	if ( strlen ( strstr ( $HTTP_REFERER, 'loja' ) ) > 0 ) { $referer = "http://".$HTTP_HOST; }
	else { $referer = $HTTP_REFERER; }
	session_register ( "referer" );
}
else {
	if ( strlen ( strstr ( $HTTP_REFERER, 'loja' ) ) == 0 ) { $referer = $HTTP_REFERER; }
	session_register ( "referer" );
}

$wOperacao = $action;
$wIdConfigurador = $idConfigurador;
$wCodproduto = $prod;
$wQuantidade = $qtd;

if ( !$preco ) {
	$wPreco = 0;
}
else {
	$wPreco = $preco;
}
$wTotal = $total;

if( $wOperacao ) {
	$ceq = 1;
}
else {
	$ceq = 0;
}

if ( !$opcionais )  {
	for ( $x = 0; $x < sizeof ( $opcional ); $x++ ) {
		if ( $opcional[$x] != "-" and strlen ( $opcional[$x] ) > 0 ) $wOpcionais .= $opcional[$x]."**";
	}

	$wOpcionais = substr ( $wOpcionais, 0, strlen ( $wOpcionais ) - 2 );
}
else {
	$wOpcionais = $opcionais;
}

Code: Select all

PHP Notice:  Undefined variable: cet in /var/www/html/loja/comprar.php on line 8
PHP Notice:  Undefined variable: action in /var/www/html/loja/comprar.php on line 25
PHP Notice:  Undefined variable: idConfigurador in /var/www/htmlloja/comprar.php on line 26
PHP Notice:  Undefined variable: total in /var/www/html/loja/comprar.php on line 35
PHP Notice:  Undefined variable: opcionais in /var/www/html/loja/comprar.php on line 44
PHP Notice:  Undefined variable: opcional in /var/www/html/loja/comprar.php on line 45
PHP Notice:  Undefined variable: wOpcionais in /var/www/html/loja/comprar.php on line 49
this appears after i change from php 4 to php 5.1 ...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Here's the problem. You're checking the values of $cet and $referrer before they have any values at all. Where are you setting their values?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:Here's the problem. You're checking the values of $cet and $referrer before they have any values at all. Where are you setting their values?
Judging from the code, bouncer is assuming register globals is on and they are therefore set for him at page submission.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

feyd wrote:
superdezign wrote:Here's the problem. You're checking the values of $cet and $referrer before they have any values at all. Where are you setting their values?
Judging from the code, bouncer is assuming register globals is on and they are therefore set for him at page submission.
you are right, i have register globals on, but i have already try something like this

Code: Select all

if ( isset ( $cet ) ) { ... }
without any progress :cry:

i know that i can hide this notices by editing php.ini, but i dont get any kind of error ... :?

the code isnt supose to work with PHP Notices ?
how can i get ride of this notices ?

thanks in advance
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to set $cet equal to something before calling it.

Code: Select all

<?php
$cet = false;

if (isset($_POST['cet']))
{
  $cet = $_POST['cet'];
}

if ($cet)
{
  //...
}
?>
Don't settle for shutting the error up when you can actually make your code error free.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks :wink:
Post Reply