Re: Undefined index error
| From: | Jason Pruim | Date: | Mon, 19 Jan 2009 20:39:57 +0000 |
| Subject: | Re: Undefined index error | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-286713@lists.php.net to get a copy of this message | ||
On Jan 19, 2009, at 3:28 PM, Daniel Brown wrote:
On Mon, Jan 19, 2009 at 14:32, Richard Heyes <richard@php.net> wrote:Only when you log into certain accounts and view the file after I had made changes :PWhen I view that file, on line 19, I see: echo <<<HTMLNotice: Undefined index: 2 in /home/raosetc/public_html/purl/p.php on line 19 and here is Line 19: $data = explode("/", $_SERVER['REQUEST_URI']);
I'm not positive that does exactly what I want... What I'm really going for is given this URL: purl.mysupersite.com/test/mail "test" refers to a record in the database. "mail" refers to a file on the server which is included when "mail" is present. Otherwise the main page is used.Are you sure it's an array? Use print_r() or var_dump() on $_SERVER['REQUEST_URI'] on the preceding line. Just for testing, you could do this too: $_SERVER['REQUEST_URI'] = 'It/Works'; ...on the preceding line.Well, that wouldn't address the issue of the code he gave on the line reported, but I know what you mean, Richy. He's turning $_SERVER['REQUEST_URI'] into an array with explode() if it contains a forward-slash ("/"). What would be better in that case would be: <?php $data = isset($_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI']) > 0 ? explode('/',$_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; if(is_array($data)) { // Handle as appropriate. } ?> However, that's not the best option as:1.) REQUEST_URI may not (though it should) have *any* forwardslashes in the URI passed to PHP by the HTTP server.2.) It leaves too much other stuff to be done.Instead, check out: <?php $data = basename($_SERVER['REQUEST_URI']); echo $data; // In case you're not yet aware of what you'll receive from the use of basename() ?>