Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Notice: Undefined offset: 1

Status
Not open for further replies.

monion03

Programmer
Apr 24, 2007
7
0
0
GR
Hi

I'm trying to search a file in order to find if a userId already exists.

My problem is that when the code runs I get the proper results plus the following notice: Notice: Undefined offset: 1 in a2p8.php on line 68

line 68 is
Code:
 list($id,$password) = split(':',$inline);
.

I searched through the web and I found that solution for my problem:
Code:
 list($id,$password) = (is_array($inline))?split(':',$inline):array('','');

The notice is then disappeared but another problem occurs. Although a userId already exists in the file, a user can choose the same Id and thus I have it in the file twice.

All the code is:
Code:
$inf="A2P8.txt";
			$FILEH = fopen($inf, 'a+') or die ("Cannot open $inf");
			$found=0;
			while (!feof($FILEH) && !($found)) {
				$inline = fgets($FILEH, 4096);
				//list($id,$password) = (is_array($inline))?explode(':',$inline):array('','');
				list($id,$password) = split(':',$inline);
				if ($id == $userId) {
				  $found = 1;
				} 
			}
			if ($found == 1) {
				print "Username: $userId already exists! Please specify another.";
			}
			else {
				$msg="$userId:$pwd"."\r\n";
				fputs($FILEH, $msg);
				print "Login succefull: userID: $userId, password: $pwd";
			}
	    
			fclose ($FILEH);

Does anybody know how to solve these problems?

Thanks a lot
 
The error you are receiving would indicate that key 1 in the array does not exist i.e. the $password variable. Are there instances where a user does not have a password? What does the line of data you're grabbing look like?

I'm not sure if this will make any difference but it might be worth a try:

Code:
$data = split(":", $inline);
$id = $data[0];
$password = array_key_exists(1, $data) ? $data[1] : "";
 
I suspect you have a blank line somewhere in your file. or a trailing carriage return at the end of it.

i would also avoid using split and instead use explode.

and adapting MSRez's code slightly (and making the assumption that a password is necessary):

Code:
$data = explode(":", $inline);
if (count($data) === 2){
  $found = ($data[0] == $userid);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top