Re: Undefined index error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 19, 2009 at 14:32, Richard Heyes <richard@xxxxxxx> wrote:
>> Notice: Undefined index: 2 in /home/raosetc/public_html/purl/p.php on line
>> 19
>>
>> and here is Line 19:
>>
>> $data = explode("/", $_SERVER['REQUEST_URI']);

    When I view that file, on line 19, I see:

echo <<<HTML


> 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* forward
slashes 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()
?>

-- 
</Daniel P. Brown>
daniel.brown@xxxxxxxxxxxx || danbrown@xxxxxxx
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux