I'm finally learning some PHP. My wife got me Larry Ullman's PHP anMySql for Dynamic Web Sites.
Well I got to the part on varibles. Tried to put $PHP_SELF and $HTTP_USER_AGENT into a script.
Showed nothing.
Apparently, this is an older book, and after some investigating, I think my problem is that the register_globals is turned off.
But how could I go about turning it on?
How are you using the codes? Show us your code, it may help.
Sure, here it is:
| Quote: |
<?php
# Script 1.6
echo "You are running the file <b>$PHP_SELF</b>.<br /><br />\n";
//echo user info
echo 'You are viewing this page with:<br /><b>', $HTTP_USER_AGENT,'</b><br />
from the IP Address', $REMOTE_ADDR;
?>
|
The text shows up, but not the varibles.
You should try this:
| Code: |
<?php
$ip = $REMOTE_ADDR;
$file = $PHP_SELF;
$browser = $HTTP_USER_AGENT;
# Script 1.6
echo "You are running the file <b>$file</b>.<br /><br />";
//echo user info
echo 'You are viewing this page with:<br /><b>', $browser'</b><br />
from the IP Address', $ip
?> |
Try that and see if anything happens.
| Phil wrote: |
I'm finally learning some PHP. My wife got me Larry Ullman's PHP anMySql for Dynamic Web Sites.
Well I got to the part on varibles. Tried to put $PHP_SELF and $HTTP_USER_AGENT into a script.
Showed nothing.
Apparently, this is an older book, and after some investigating, I think my problem is that the register_globals is turned off.
But how could I go about turning it on? |
Place the following code in a .htaccess file to turn register_globals on:
| Code: |
| php_flag register_globals on |
| Phil wrote: |
| Apparently, this is an older book, and after some investigating, I think my problem is that the register_globals is turned off. |
Yes, that is a problem with old books (and a sign of a sloppy writer). The default has been for register_globals to be on until PHP version 4.2.0; but relying on it has been a bad practice even in those days (before August, 2000!).
| Phil wrote: |
| But how could I go about turning it on? |
Rather than turning them on, use the brand new (since December, 2001; with PHP 4.1.0) superglobal arrays. Also be aware the register globals does not exist in PHP version 6 and, if you start using it now, you will have to change your scripts when no more hosts have PHP 5 installed.
Instead of | Phil wrote: |
| Code: | <?php
# Script 1.6
echo "You are running the file <b>$PHP_SELF</b>.<br /><br />\n";
//echo user info
echo 'You are viewing this page with:<br /><b>', $HTTP_USER_AGENT,'</b><br />
from the IP Address', $REMOTE_ADDR;
?> |
|
code your scripts as | Code: |
<?php
# Script 1.6
echo "You are running the file <b>{$_SERVER['PHP_SELF']}</b>.<br /><br />\n";
//echo user info
echo 'You are viewing this page with:<br /><b>', $_SERVER['HTTP_USER_AGENT'],'</b><br />
from the IP Address', $_SERVER['REMOTE_ADDR'];
?> |
You can see all the info available in the superglobals with | Code: |
<?php
echo '<pre>';
echo "\$_SERVER: "; print_r($_SERVER);
echo "\n\n\$_SESSION: "; print_r($_SESSION);
echo "\n\n\$_GET: "; print_r($_GET);
echo "\n\n\$_POST: "; print_r($_POST);
echo "\n\n\$_COOKIE: "; print_r($_COOKIE);
echo "\n\n\$_FILES: "; print_r($_FILES);
echo "\n\n\$_ENV: "; print_r($_ENV);
### echo "\n\n\$_REQUEST: "; print_r($_REQUEST);
### $_REQUEST is a mix of $_GET, $_POST and $_COOKIE
echo '</pre>';
?> |
Like hexkid sugest it is much better to use superglobals then registerglobals. Old is old and it is better to kick out...
Sonam
So this book I got, Larry Ullman's PHP and MySql for Dynamic Web Sites is no good? Looking at the cover, it covers PHP 3 and 4. From your replies, php is up to 6 now?
Should I just ditch the book?
| Phil wrote: |
| So this book I got, Larry Ullman's PHP and MySql for Dynamic Web Sites is no good? Looking at the cover, it covers PHP 3 and 4. From your replies, php is up to 6 now? |
The latest release is 5.2.0 (release announcement); you can get PHP 6 only from the snapshots.
| Phil wrote: |
| Should I just ditch the book? |
No. But expect some of the code needs tweaking for the newer versions of PHP. Mainly it's the uninitialized variables which you can be notified of by PHP itself by adding the line in my sig to the scripts from the book.
| Code: |
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
### script from the book
echo "Hello visitor from $REMOTE_ADDR.";
?> |
In the code above, PHP will notify you about the use of uninitialized $REMOTE_ADDR variable, so you may assume it's because of the version differences: just replace it with the proper superglobal array.
| Quote: |
| Phil wrote: | | Should I just ditch the book? | No. But expect some of the code needs tweaking for the newer versions of PHP. .
|
Ok, I've been messing around with all the examples you guys have given. The best thing that has worked is putting {$_SERVER['WHAT_EVER']} for the variables.
Is it that simple for variables? Just adding {$_SERVER[*]}?
Also, I noticed everything is uppercase. Is that important?
| Phil wrote: |
The best thing that has worked is putting {$_SERVER['WHAT_EVER']} for the variables.
Is it that simple for variables? Just adding {$_SERVER[*]}? |
Hmmm ... no. It depends. If you're trying to display a cookie value | Code: |
<?php
set_cookie('foobar', 'forty two');
echo "The cookie 'foobar' has the value $foobar.<br>";
echo "Please refresh the page if it is blank.";
|
you need to use $_COOKIE instead. If you're dealing with query parameters, you need $GET; and you need $_POST for form fields. Other than these, there's the very much used $_SESSION too. With a bit of experience it's easy to know which one to use
| Phil wrote: |
| Also, I noticed everything is uppercase. Is that important? |
VERY IMPORTANT PHP is (mostly) a case sensitive language.
$value is not the same as $Value or $VALUE
$_SERVER['foobar'] is not the same as $_SERVER['FOOBAR']
... but for functions it (strangely IMHO) isn't, so
$now1 = date('Y-m-d H:i:s');
$now2 = Date('Y-m-d H:i:s');
$now3 = DATE('Y-m-d H:i:s');
are all ok.
Although the newest official PHP release is indeed 5.x, a lot of hosts (including frihost) still use PHP 4. But, this means we're now in the PHP 4/5 times, and that book is from the 3/4 times, so it still is an old book. But old != useless. Almost everything in PHP 3 is still available in PHP 5 (though there are exceptions), but better ways to do these things may exist.