The online racing simulator
Simple PHP question
1
(46 posts, started )
Simple PHP question
I'll admit to being rather inexperienced with PHP, with it being years since I've really used it and even then I found it a pain. Once again I'm struggling and google isn't helping.

All I want to is append the users IP address to a string.

I can append to a string fine:
$mailbody .= "IP address:"

I can print out the IP address:
echo $_SERVER['REMOTE_ADDR'];

But can't get the IP address in the string. The page just won't load (I get an empty browser).

I've tried:
$mailbody .= "IP address: $_SERVER[REMOTE_ADDR]\n";

And various other subtly different cominations, to no avail.

What gives?
$mailbody .= "IP Address: " . $_SERVER['REMOTE_ADDR'];

Or something like that.
You're missing the quotes around the array key? So PHP thinks you're referencing a constant, I guess, and can't find it.
Thanks, worked straight away. I really can't see the difference between what you posted and what I'd tried. I had the single quotes in at one point, and didn't have any text so no doubles either.

Edit: just to show I'm not going mad, the code I posted but with single quotes in definitely doesn't work. So what else is wrong with it?

Edit again: hmm, never mind, I went back to my simplest example and it worked. Bloody web coding.
$mailbody .= "IP address: $_SERVER['REMOTE_ADDR']\n";
or
$mailbody .= "IP address: ".$_SERVER['REMOTE_ADDR']."\n";
?

beaten.. by a long way
#6 - Ian.H
Be aware Bob, that the REMOTE_ADDR value may not be what you want or are expecting, for example, some ISPs route all their traffic through their own caching servers. REMOTE_ADDR will return the IP of the caching server, _not_ the visitor's box.

Here's an old function I wrote to help:


<?php 
function GetIP() {
    
$ipAddy '';

    if (isset(
$_SERVER['HTTP_X_FORWARDED_FOR']) and trim($_SERVER['HTTP_X_FORWARDED_FOR']) != '') {
        
$ipAddy trim($_SERVER['HTTP_X_FORWARDED_FOR']);

        if (isset(
$_SERVER['REMOTE_ADDR']) and trim($_SERVER['REMOTE_ADDR']) != '') {
            
$ipAddy .= ' ' trim($_SERVER['REMOTE_ADDR']);
        }
    } else {
        if (isset(
$_SERVER['REMOTE_ADDR']) and trim($_SERVER['REMOTE_ADDR']) != '') {
            
$ipAddy trim($_SERVER['REMOTE_ADDR']);
        }
    }

    return 
$ipAddy;
}
?>

This first checks for a vale in HTTP_X_FORWARDED_FOR which should be the "real IP" if behind some kind of cache etc, if that's empty, then it checks for REMOTE_ADDR, if it's not, it still checks for REMOTE_ADDR and will append that IP to the result separated by a space.


<?php 
$mailbody 
.= 'IP Address: ' GetIP();
?>





Regards,

Ian


PS: NAI: Isn't the first one missing curly brackets?


<?php 
$mailbody 
.= "IP Address: {$_SERVER['REMOTE_ADDR']}";
?>

Although putting $vars inside quotes is a "bad idea(tm)" anyway



Regards,

Ian
Ah, thanks Ian, looks handy that.

Is there a way to turn some sort of debug messages on? Every time I add something and I've done it wrong I'm getting this damn unhelpful white screen.
Quote from Ian.H :
PS: NAI: Isn't the first one missing curly brackets?

Could well be, I didn't check them, and I'd use the latter anyway. If not for the right reasons, it makes it easier to spot the $vars
Quote from Bob Smith :Ah, thanks Ian, looks handy that.

Is there a way to turn some sort of debug messages on? Every time I add something and I've done it wrong I'm getting this damn unhelpful white screen.

Sure.. you can either set it in php.ini and / or within each script.

If the script way, include this at the top of each one (or preferably, in an include()ed file so that it can be enabled / disabled in one place rather than every file):


<?php 
error_reporting
(E_ALL E_NOTICE);
?>

That'll enable warnings _and_ notices (ie: use of uninitialised vars etc for strictness).

If in php.ini, look for the lines:

error_reporting = E_ALL|E_NOTICE

...

display_errors = On

(just search for the strings as yours may well be 'Off' and / or just 'E_ALL' etc).


Remember to restart Apache if you modify php.ini for it to take effect



Regards,

Ian
Quote from NotAnIllusion :Could well be, I didn't check them, and I'd use the latter anyway. If not for the right reasons, it makes it easier to spot the $vars

heh actually, single quotes would be even better.. more optimised :nerd:



Regards,

Ian
Ian - care to explain why using a variable directly in quotes is a bad idea?
Because PHP has to parse the string.
Single quotes for performance (simple strings, no parsing) and double quotes for readability (always get parsed, even if there's nothing to be parsed).

It's a geek thing really, the difference in performance is immeasurable IMHO
I'm that kind of a geek though, I only use double quotes for control characters. I could use chr() but that's another (explicit) function call and usually slower
Ok, Thanks I just wanted to know, seeing I've started to learn PHP and want to know if when I do stuff if I do any major faux pas.
Quote from morpha :Because PHP has to parse the string.
Single quotes for performance (simple strings, no parsing) and double quotes for readability

Plus in any decent editor unquoted characters receive syntax highlighting. Nothing pisses me off more than vars inside quotes that I don't notice on a quick scan through of code.
Kev - The editor I use (OSX Only) is Coda. Has very good syntax highlighting, and for the most part is pretty smart when you're coding, and the blockedit that is very useful. Is a good all-in-one web development tool. Also I like the pretty colours.
Yeah Coda is pretty good.

I rarely sit infront of a Mac, but I'm planning to buy a Macbook (or "Mactop" as I like to call them) as soon as boutique pedal designers stop coming up with stuff that seems badass.

I'm quite addicted to Textpad, being a Windows user. The main reason I haven't switched to Linux is because I don't like any of its editors as much as Textpad.
Even with the knowledge that WINE is mature enough that Textpad would more than likely work in Linux? Checking Textpad out quickly, It's actually quite nice, now if it had better PHP highlighting, I'd like it more I think.
Quote from dawesdust_12 :Even with the knowledge that WINE is mature enough that Textpad would more than likely work in Linux?

I just can't be bothered with emulators (or not-emulators). I don't particularly like the Linux desktop environments, and all that choice in reality means a long time spent meandering through mediocrity and never being satisfied.

I like the Mac OS. I think it's clever and I'd like to use it. I'm just still feeling a bit like a sucker because of the hardware prices, so I haven't bought one yet.

Quote from dawesdust_12 :Checking Textpad out quickly, It's actually quite nice, now if it had better PHP highlighting, I'd like it more I think.

Did you download the latest syntax highlighting modules from their website? Every line of PHP I've written in the last 7 years (must be 100,000 or more) has been in TextPad. The main reason I've stuck with it is that it highlights everything else too, you can customise the display by file type, it does bracket matching, multi-document find/replace, PCRE pattern matching, you can trigger command lines for compilers/linkers, etc. I just haven't found a feature I want that it doesn't already do.
I found it. I'm a bit too stupid to set up better syntax colouring and stuff, and like how Coda comes setup by default. Is there away you could share your settings. (export HKEY_CURRENT_USER\Software\Helios\TextPad 5\Document Class).
If I hadn't got through a bottle of Shiraz and a very nice 2003 Rioja reserva, then maybe. Right now though, no. Not a cat in hell's chance.

Be thankful you've got proper spelling out of me. It took some effort.
Yeah, I'd suggest entering regedit while drunk would be a bad idea, only seconded to flashing your BIOS while pissed.
Quote from dawesdust_12 :Ian - care to explain why using a variable directly in quotes is a bad idea?

As morpha says, single quotes are taken literally by PHP, whatever's in them gets taken at face value. Double-quotes are assumed as possibly containing parsable data (ie: vars) so PHP actually looks to try and parse the entire string, thus being a little slower.. but again as morpha says, you'll never see it, it's just a geeky thing

Same goes for using echo rather than print to display a string. Print returns a true / false value, echo doesn't.. so effectively using echo is slightly more optimised. Again you won't notice it, especially as today's servers are getting more and more powerful, but it's there



Regards,

Ian
Quote from dawesdust_12 :Yeah, I'd suggest entering regedit while drunk would be a bad idea, only seconded to flashing your BIOS while pissed.

I've done both, the former I was so drunk I couldn't find what I wanted so stopped trying, the latter well it didn't make any difference, the BIOS update was already on an FDD so all I had to do was put it in, hit an F key after post and let the auto flasher run. Idiot proof systems FTW.
Quote from Bob Smith :$mailbody .= "IP address: $_SERVER[REMOTE_ADDR]\n";

PHP does not like parsing array variables like that.

This should work for you tho:
$mailbody .= "IP Address {$_SERVER['REMOTE_ADDR']}\n";

I'd also like to point out that I added ' (signal quotes) around the array key name REMOTE_ADDR, that might of also of been a reason. But the complex strings example should fix this explicit problem. Where as Ian.H's reply is the most robust answer for them all, and should be used over mine. Just wanted to you know the why, and the how.
1

Simple PHP question
(46 posts, started )
FGED GREDG RDFGDR GSFDG