-1

I have made an sms handler system, and everything is okey, working fine. But I get this error: PHP Notice: Undefined offset: 1 Its in the 17. line $user is the 17. line, I know its just notice, but daily 20-30 "notice" is in my php error log, and I wanted to fix this.

I tryed many different method, but no changes. Somebody can help to fix it? Thanks!

$conn           = sqlsrv_connect($serverName, $connectionInfo);
$id             = $_GET["id"];
$from           = $_GET["from"];
$to             = $_GET["to"];
$msg            = explode(" ", $_GET['message']);
$user           = substr(trim($msg[1]),0,10);
Vbnetguy
  • 57
  • 9

2 Answers2

1

Viewing this code helps less to understand but still i would recommend you to place

if(isset($msg[1]) && $msg[1] != ''){
  $user = substr(trim($msg[1]),0,10);
} else {
  $user = '';
}

because it looks like in some cases $msg[1] does not exist. For example if $_GET['message'] = 'Hello';

ZeissS
  • 11,867
  • 4
  • 35
  • 50
Ankit
  • 607
  • 2
  • 9
  • 16
0

Well, $_GET['message'] does not seem to contain a second element. Are you sure its set?

Your code should handle this nicely by having an if or sthg similar. Examples:

$user = "anonymous";
if (sizeof($msg)) > 1) {
  $user = substr(trim($msg[1]),0,10);
}
bcesars
  • 1,016
  • 1
  • 17
  • 36
ZeissS
  • 11,867
  • 4
  • 35
  • 50