Undefined 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
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Undefined Index

Post by farid »

Hi, y'all!!

I hope anyone could help me, I have a problem with some code using sessions, in the first page I initialize the variable with some value, and in the third page, I need to do a validation with such variable, but It appears two notice, wich I have never heard them before, like:

NOTICE: Undefined Index: variable in D:/mydocuments on line 74

Hope someone can help me!!! :oops:
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

do you have session_start(); on top of every page?
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Yes, and it says that a session had already been started ignoring session start :oops:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

On the third page, call print_r($_SESSION); to see what is in the $_SESSION variable. If that doesn't give you any leads, please post your code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

okey, here it goes:

page 2:

<?php //session_start(); ?>
<html>
<head>
<title>Sistema de Reporte de Ficha Amigo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body TEXT=#000000>
<div align="left"><font size="2" face="Arial"> <strong>
<?php
$_SESSION['login'] = $_POST['requiredlogin'];
$_SESSION['password'] = $_POST['requiredpassword'];

$login = $_SESSION['login'];
$password = $_SESSION['password'];
$_SESSION['activa'] = 0;

if($_SESSION['activa'] == 0){

$conn = mssql_connect("ip", "seguridad", "angangueo92") or die("Connection Failed");

$select_db = mssql_select_db("sisecgsm", $conn) or die("Connection Failed");

$host = $_SERVER['REMOTE_ADDR'];

$id_proyecto = 15; //ASIGANCIÓN DE VALOR ID_PROYECTO
$base = mssql_select_db("sisecgsm", $conn) or die ("Couldn´t connect");

$stmt = mssql_init("sp_validaseguridad", $conn);
mssql_bind($stmt, "@ip", $host, SQLCHAR, false);
mssql_bind($stmt, "@iproyid", $id_proyecto, SQLINT2, false);
mssql_bind($stmt, "@slogin", $login, SQLVARCHAR, false);
mssql_bind($stmt, "@spass", $password, SQLVARCHAR, false);

$result = mssql_execute($stmt);
$arr = mssql_fetch_array($result);

$error = $arr['error'];

if ($error == 0){
$_SESSION['pass_sms'] = $arr['passbd'];
$_SESSION['nombre'] = $arr['nombre'];
$_SESSION['nivel'] = $arr['nivel'];
$_SESSION['grupo_rep'] = $arr['grupo'];
$_SESSION['segmento'] = $arr['segid'];
$_SESSION['perfil'] = $arr['perfil'];
$_SESSION['status'] = $arr['status'];
$_SESSION['contador'] = $arr['contador'];
print_r($_SESSION);
if ($_SESSION['activa'] != 1){
$_SESSION['contador']++;
$c = $_SESSION['contador'];
$qr = "UPDATE sisec_proyectos SET contador=$c WHERE proy_id=$id_proyecto";
if (!mssql_query($qr, $conn))
$_SESSION['activa'] = 1;
}

if (valida($_SESSION['perfil'], 0))
redir("pantalla_scltmc.php");
else if (valida($_SESSION['perfil'], 1))
redir("pantalla_gcadcom.php");
else if (valida($_SESSION['perfil'], 2))
redir("pag__admin_principal.php");
else
redir("pag_principal.php");
}
else{
$result1 = mssql_query("SELECT * FROM sisec_errores WHERE err_id = '$error'", $conn) or die("Connection Failed");
$query_data1 = mssql_fetch_array($result1);
$password_bd = "Nada";
$_SESSION['activa'] = 0;
echo $query_data1['err_nombre']
?>
<?php }
/* unset($stmt);
mssql_close($conn);
*/}?>

</strong></font> </div>
</body>
</html>

<?php
function valida($perfil, $valor)
{ // Función que valida el perfil para el redireccionamiento
if ($perfil[$valor] == 'A')
return 1;
return 0;
}

function redir($url)
{
$delay = "2"; // 1 second delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
echo "Redireccionando: ".$url." PERFIL: ".$_SESSION['perfil'];
}
?>

page3:

<?php //session_start();
unset($_SESSION['clasificacion']);
unset($_SESSION['clasificacion_txt1']);
unset($_SESSION['clasificacion_txt2']);
unset($_SESSION['clasificacion_txt3']);
unset($_SESSION['clasificacion_txt4']);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Sistema de Reporte de Ficha Amigo</title>
<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif}
.style3 {font-family: "Lucida Sans Unicode"; color: #FFFFFF; }
.style5 {color: #FF0000}
-->
</style>
</head>

<body>
<p align="center"><font color="#000000" size="2" face="Arial">&nbsp;
<?php
print_r($_SESSION);
$perfil=$_SESSION['perfil'];

if($_SESSION['activa']!=1){
echo $advertencia="SESION NO ACTIVA", "<br><br>";
redir("pag_principal.php");
}

if(!valida($perfil, 0)){
echo $advertencia="ACCESO NO PERMITIDO", "<br><br>";
redir("pag_principal.php");
}
?>
<?php
function valida($string, $valor){ // Función que valida el perfil para el redireccionamiento
if ($string[$valor]=='A'){
return 1;
}
else{
return 0;
}
}
function redir($url)
{
$delay = "2"; // 1 second delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
echo "Redireccionando: ".$url." ";
}
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

farid wrote:<?php //session_start(); ?>
I think I see the problem.. :roll:
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

if I let like

<?php session_start();?> it says that the session is already started and it ignores it
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Oh. Ok then. I really ought to read threads before commenting I guess. :)

You're going to have to use print_r($_SESSION) a lot to find out where you're trying to change a session variable before you've set it. I looked at the code in a PHP editor, and couldn't quite work out how you're getting an error on line 74 in page3 .. it's only 52 lines long.
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

oh oooh, sorry, it seems like to be 52 cause I edited a quit bit, but it sure is the line where the session variables are, that is the activa and perfil variables, it seems like a case for the twilight zone.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Try wrapping a test for whether $_SESSION['activa'] is set around the block where you check it's value.

Code: Select all

if (isset($_SESSION['activa']) {
     if($_SESSION['activa']!=1){
          echo $advertencia="SESION NO ACTIVA", "<br><br>";
          redir("pag_principal.php");
     }
}
I think the problem is that your PHP error level is set so low that you're getting NOTICEs echo'd out, and that's mucking up the output. Technically, checking an array index that isn't set really isn't a problem, so you should be able to safely ignore the problem. But as the error level is set to tell you about it you're going to need to code around it.
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Okey man I´ll check it out, Thanks for your help!! :D :P Apreciate it!! See ya!!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Also, always wrap any code you post here in [syntax=php][/syntax] tags. That makes the code colourized and maintains indentation - much, much, much easier to read.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Sure man!! I will, sorry 'bout it, and again Thanks!!

:D
Post Reply