Forum Moderators: coopster

Message Too Old, No Replies

Undefined Offset

         

Saboi

12:54 pm on Dec 14, 2010 (gmt 0)

10+ Year Member



Hello guys,

I have the following code and i need assistance getting over the undefined offset error.

Here is the logic. Someone types in something like Bank[space]forename surname. This they send to the php code for processing.

I get undefined offset errors when message sent does not have the three needed variables. I seek advise on how to work around this. Would like to know how input even with one variable can be handled without displaying the undefined offset message.


<?php
02
03/* Getting sender's phone number and message text */
04
05$price = "k230,000";
06
07$price2 = "k1.8m";
08
09/*$senderPhone = $_POST['sender'];*/
10
11$messageText = $_POST['message'];
12
13$variables = explode(" ","$messageText ");
14
15$msg_code = strtoupper($variables[0]);
16
17$forename = ucfirst($variables[1]);
18
19$surname = ucfirst($variables[2]);
20
21$message1 = "Hello $surname";
22
23$message2 = "Hello $forename";
24
25$message_type = "header('Content-Type: text/plain; charset=utf-8')";
26
27if( $msg_code == ""){ $note = "Invalid SMS"; exit();}
28if( $forename == "") { $note = "Invalid SMS"; exit();}
29if( $surname == "") { $note = "Invalid SMS"; exit();}
30
31if ($msg_code == "Bank")
32
33 {
34
35 if( $forename == "") { exit();}
36
37 if( $surname == "") { $note = "Invalid SMS"; exit();}
38
39 else
40
41 { $file = "bank.txt";
42
43 $file_content = file_get_contents($file);
44
45 $addition = $file_content + 1;
46
47 if($file_content<=3)
48
49 {
50
51
52 $senderPhone;
53 $message_type;
54 echo $message1;
55
56 }
57 }
58
59
60 }
61
62elseif($msg_code == "airport")
63
64 {
65
66 if( $forename == "") { $note = "Invalid SMS"; exit();}
67
68 if( $surname == "") { $note = "Invalid SMS"; exit();}
69
70 else
71
72 { $file = "airport.txt";
73
74 $file_content = file_get_contents($file);
75
76 $addition = $file_content + 1;
77
78 if($file_content<=3)
79
80 {
81
82 $senderPhone;
83 $message_type;
84 echo $message2;
85
86 }
87 }
88
89
90 }
91
92
93
94?>


coopster

3:47 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



input type checkbox on a form will not return the variable name if nothing was checked and then the form was submitted. You need to check for the existence of a (potential) user-supplied variable before using it otherwise you will get warning errors. Use the PHP isset() function to check for the existence of an index (offset) in your arrays.

rocknbil

4:31 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




list($errors,$msg_code,$forename,$surname) = check_message($messageText);
if ($errors) { echo $errors; exit; } // or return to form.
//
function check_message ($vars) {
if (! preg_match('/[\d\w]+/',$vars)) { return "<p>No value for message text.</p>"; }
$vars = explode(" ",$vars);
$errList = null;
for ($i=0;$i<3;$i++) {
$non_zero_index=$i+1;
if (! isset($vars[$i]) or ! preg_match('/[\w\d]+/i',$vars[$i])) {
$errList .= "<p>Variable number $non_zero_index is missing.</p>";
}
}
if ($errList) { return $errList; }
else { return ($errList,strtoupper($vars[0]),ucfirst($vars[1]),ucfirst($vars[2]); }
}