Page 1 of 1

Undefined index: [Problem Solved]

Posted: Tue Dec 05, 2006 11:38 am
by victhour
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello guys,

i wonder what goes wrong with my code which involved sessions.. and received the following message bellow.

[quote]Notice: Undefined index: [b]some message line whatever # here[/b]
The search identifier had errors.[/quote]

here's my LogIn script that will then process to the index.php page...

Code: Select all

<?php
require("../modules/config.php");
include("../modules/modlib.php");

if($_POST['cmdLog']) // On POST execute statments.
{
	$user = escval($_POST['per_user']);
	
	$seek = "SELECT usr FROM user_mas WHERE usr = '$user'";
	$flag = mysql_query($seek) or die('Problem:'.mysql_error());

	$getusr = mysql_fetch_array($flag);

	$pass = encrypt_pass($_POST['per_pass']);

	if(strcmp($user, $getusr['usr'])){
		echo '<script type="text/javascript">
						alert("Invalid User or Password!")
						window.location.href = "login.html"
					</script>';
		die();
	}else{
		$seek = "SELECT user_mas.id_mas, user_mas.usr, user_mas.auth, user_dtl.nme FROM user_mas, user_dtl 
						 WHERE user_mas.usr = '$user' AND user_mas.psw = '$pass' AND user_mas.id_mas = user_dtl.id_dtl";
		$flag = mysql_query($seek) or die('Problem: '.mysql_error());
		$getrow = mysql_fetch_array($flag);

		if(mysql_num_rows($flag) == 1){ 
			// Process if true
			session_start(); 
			$_SESSION['ses_id'] = genid($_SESSION['ses_id']);
			$_SESSION['usr_id'] = $getrow['id_mas'];
			$_SESSION['usr_op'] = $getrow['auth'];
			header('Location: ../index.php');
		}else{
			echo '<script type="text/javascript">
							alert("Invalid User or Password!")
							window.location.href = "login.html"
						</script>';
			die();
		}
	}
}// End POST statements.
?>
and here's is the index.php that will supposed to recognized the sessions from the login script.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SuperBalita Content Management System v1.0</title>
<script type="text/javascript" src="modules/jscript.js"><!-- //--></script>
<script type="text/javascript">
<!--
function validateForm(form) {
  if(isChosen(form.mnuLocation, 'Location')){
		 if(isChosen(form.mnuSection, 'Section')){
		 		if(isNotEmpty(form.fldTitle, 'Title')){
					if(isNotEmpty(form.fldContent, 'Description')){
						return true;
					}	
				}	
  	 }
  }
	return false;
}
//-->
</script>
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php

session_start();
$_SESSION['ses_id'];
$_SESSION['usr_id'];
$_SESSION['usr_op'];
if(!isset($_GET['ses_id'])){
	die("The search identifier ".$_GET['ses_id']." had errors.");
}

if(!isset($_GET['usr_id'])){
	die("The search identifier " . $_GET['usr_id'] . " had errors.");
}

if(!isset($_GET['usr_op'])){
	die("The search identifier " . $_GET['usr_op'] . " had errors.");
}
?>
<div id="wrap">
  <div id="divSet">
    <div class="detail">
      <div id="user">SignOut: <b><a href="admin/log_off.php">User Level 1</a></b> - <a href="#">View Profile</a></div>
      <div id="date">
        <script language="JavaScript" type="text/javascript">dT();</script>
      </div>
    </div>
    <div style="clear: both;">
      <div class="lefColm">
        <p class="ctrl"><a href="#?url_id=">Gateway</a> | <a href="#">Layout Mananger</a>	| <a href="#">Archieve Manager</a> </p>
      </div>
      <div class="ritColm">
        <p class="ctrl"><a href="#">User Manager</a> | <a href="#">Database Panel</a> | <a href="#">Data Overview</a></p>
      </div>
    </div>
    <div style="background-color: #666699; clear: both;">
      <div style="padding: 1px 2px;">
        <p class="ctrl"><a href="modules/mod_write.php" target="contentFRM">Add Article</a> |<a href="#">View Article</a> </p>
      </div>
    </div>
    <div style="background-color:#CCCC99; padding: 10px; clear: both;" align="center">
      <!-- Module Goes Here -->
      
			<?php include('modules/mod_write.php'); ?>
			<?php //include('modules/iframe.html'); ?>

      <!-- End Module -->
    </div>
  </div>
</div>
</body>
</html>
anybody have some idea please help

thanx in advanced..


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Dec 05, 2006 11:47 am
by feyd
Considering the number of index references you have in your code, it would help to know the exact error you are seeing if you want specific help fixing that problem.

The problem comes from assuming an array element exists when it does not. isset() and array_key_exists() are often used to detect the existence of the element before it is used as to avoid or fix code that would otherwise cause errors such as this.

Posted: Tue Dec 05, 2006 11:56 am
by ok
You can't write vars alone:

Code: Select all

session_start();
$_SESSION['ses_id'];
$_SESSION['usr_id'];
$_SESSION['usr_op']; 
Maybe (added feyd recommendations):

Code: Select all

session_start();
if(isset($_SESSION['ses_id']) and $_SESSION['ses_id'] != null)
{
    echo "id set!";
}
if(isset($_SESSION['usr_id']) and $_SESSION[''usr_id'] != null)
{
    echo "id set!";
}
if(isset($_SESSION['usr_op']) and $_SESSION['usr_op'] != null)
{
    echo "id set!";
}
BTW, in the login script:

Code: Select all

$_SESSION['ses_id'] = genid($_SESSION['ses_id']);
Where you set $_SESSION['ses_id']???

Posted: Tue Dec 05, 2006 1:27 pm
by victhour
feyd:
Considering the number of index references you have in your code, it would help to know the exact error you are seeing if you want specific help fixing that problem.
i received something like:
Notice: Undefined index: ses_id in C:\Web Root\script\index.php on line 34
The search identifier had errors.

then i try to remove $_SESSION['ses_id'], it will just move to the next Undefined index 'usr_id' and so on...

ok

Code: Select all

$_SESSION['ses_id'] = genid($_SESSION['ses_id']);
"genid" is a function that will randomly generate some "id" to assign session.

Posted: Tue Dec 05, 2006 1:32 pm
by ok
victhour wrote:"genid" is a function that will randomly generate some "id" to assign session.
You don't need to generate a session id, PHP does it for you.

Posted: Tue Dec 05, 2006 1:58 pm
by victhour
"genid" is their to give some value to $_SESSION['ses_id'], like $_SESSION['usr_id'] = $getrow['id_mas']; where 'id_mas' is a some key from a database.

im not sure if i explained it correctly.. coz my english is not that fluent :?

by the way. i check the "session file" generated, and there values some thing like:

Code: Select all

ses_id|s:36:"47f326c588ec3186d0b10ef608ca460d1840";usr_id|s:1:"1";usr_op|s:1:"A";
im confused why i received such Notice where they have an actual values...

Posted: Tue Dec 05, 2006 2:21 pm
by ok
Notice: Undefined index: ses_id in C:\Web Root\script\index.php on line 34
The search identifier had errors.
Which line is 34? Post the line from the code.

Posted: Tue Dec 05, 2006 2:37 pm
by feyd
The following line will generate a notice when the session is first created. It attempts to read a session variable ($_SESSION['ses_id']) when it doesn't exist yet.

Code: Select all

$_SESSION['ses_id'] = genid($_SESSION['ses_id']);
You need to use isset() or array_key_exists() to verify that the value exists before calling genid().

Posted: Tue Dec 05, 2006 2:40 pm
by ok
feyd wrote:The following line will generate a notice when the session is first created. It attempts to read a session variable ($_SESSION['ses_id']) when it doesn't exist yet.

Code: Select all

$_SESSION['ses_id'] = genid($_SESSION['ses_id']);
You need to use isset() or array_key_exists() to verify that the value exists before calling genid().
He is right. I will translate feyd words into PHP:

Code: Select all

if(isset($_SESSION['ses_id']) and $_SESSION['ses_id'] != null)
{
 $_SESSION['ses_id'] = genid($_SESSION['ses_id']);
}
BTW, why you pass $_SESSION['ses_id'] to genid??? It is not suppose to generate a session id???

Posted: Wed Dec 06, 2006 12:26 am
by victhour
tanx guys... problem solved("i think") :)

Posted: Wed Dec 06, 2006 2:30 am
by ok
If you can, post the solution so other also will know how you solved it. :D

Posted: Wed Dec 06, 2006 2:53 am
by victhour
this are the changes

Code: Select all

session_start();
if(!isset($_SESSION['ses_id']) && $_SESSION['ses_id'] == NULL){
	die("The search identifier ".$_SESSION['ses_id']." had errors.");
}

if(!isset($_SESSION['usr_id']) && $_SESSION['usr_id'] == NULL){
	die("The search identifier ".$_SESSION['usr_id']." had errors.");
}

if(!isset($_SESSION['usr_op']) && $_SESSION['usr_op'] == NULL){
	die("The search identifier ".$_SESSION['usr_op']." had errors.");
}
thanks you guys for participation to this thread...