Web Hosting Talk







View Full Version : Simple PHP Code that I can't figure out


jtrovato
05-29-2003, 03:00 AM
This is the code, Simple enough


<?php
echo $_SERVER['HTTP_USER_AGENT'] . "<hr />\n";

$browser = get_browser();

foreach ($browser as $name => $value)
{
print "<b>$name</b> $value <br />\n";
}

?>


This is the error I get:

Warning: Invalid argument supplied for foreach() in /home/johntrov/public_html/index.php on line 24

Line 24 would be at the foreach statement

I enabled the browscap.ini too

??? no clue

Constantin
05-29-2003, 03:15 AM
You should check if in your case $browser is an array. Probably not.

jtrovato
05-29-2003, 03:23 AM
This code is from the php.net site.

Weird how it doesn't work correctly.

Constantin
05-29-2003, 03:30 AM
It works for me (Apache 1.3, PHP 4.3.1, Win2k).

Have you restarted the HTTP server after you changed php.ini to enable browscap.ini?

Rich2k
05-29-2003, 04:42 AM
I don't like relying on browscap.ini as it's too easy to fake user-agents.

jtrovato
05-29-2003, 12:50 PM
i did restart. I'm just using it for testing purposes...

Do you have a better way of doing it?

Sheps
05-29-2003, 02:13 PM
Your foreach syntax is wrong... Try:


while( list($name,$ value) = each($browser) )
{
print "print stuff here";
}

plugged
05-29-2003, 03:53 PM
read the php.net page,

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP but you may find an up-to-date browscap.ini file here. By default, the browscap directive is commented out.

http://us3.php.net/get_browser

jtrovato
05-29-2003, 07:26 PM
Yeah, I checked the path in the php.ini file. to point to the correct browscap.ini file...

That seems to work fine

The get_browser returns an object

the foreach comment displays each field within an array, not an object? I could be wrong. I'm so a C++ programmer

So basically I want to display all the objects variables and their names out to the browser.

Thanks for the help