Webhost upgraded to PHP5 - can't print $myvar on second page

Discussion in 'PHP' started by newlook, Nov 27, 2006.

  1. #1
    Hopefully, someone can point out what's wrong or what I'm missing. My husband's webhost upgraded to PHP5, and now everything's a mess. I'm having problems with variables not being registered. But I've been trying different things and things just seem weird. (Note: things were coded a long time ago, when I was a newbie, with register globals on -- the webhost turned that to off awhile ago, so we were setting it to on through php.ini file -- I know it shouldn't be on;there just hasn't been time rewrite things...)

    Anyway, as a simple test, I tried this:

    I have page1.php with this, say, this for example:
    =====================
    <?php
    session_start();
    $myname = "John";
    session_register('myname');
    ?>

    <a href="page2.php">Go to page 2</a>
    =====================

    And this on page2.php:
    =====================
    <?php
    session_start();
    print "My name is: $myname";
    print "<br> but using $*_SESSION['myname'], I get: ".$_SESSION['myname'];
    ?>
    =====================

    The output on page2 (when coming from page1) is:
    My name is:
    but using $*_SESSION['myname'], I get: John

    I'm not crazy, right -- shouldn't I at least be getting a value when printing $myname?

    Is there something wrong on my side or was something missed when PHP5 was compiled by my webhost? :confused: This should be so simple... register a variable and then use it throughout...

    Thanks in advance...
     
    newlook, Nov 27, 2006 IP
  2. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not familiar with the * in $*_SESSION['myname'], is that a typo or another function im not aware of?

    Also, you should escape it out of the print with ".." too, shouldn't you?

    In regards to my name, isnt there a register global settings that prevents/allows that? this may be your new setting.
     
    Luke, Nov 27, 2006 IP
  3. newlook

    newlook Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Luke,

    No, I was just using $*_SESSION with an asterisk, so that it would print out. If you look at the code, I'm using $_SESSION['myname'] to print out the myname variable.

    Yup, register_globals is set to off and has been for awhile. We worked around that by putting a register_globals on in a php.ini file; this worked fine until now.

    Unless things have changed in a major way, there's nothing wrong with: print "hello world $some variable";

    Thanks!
    ================

    To ALL: I put the exact same test code on another server, and had no problems with printing $myname.

    Would this have anything to do with session save path? It's currently set to "/"

    Thanks...
     
    newlook, Nov 27, 2006 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Still sounds like a register globals thing to me. Have you checked your error logs? Is there a reason you don't want to use $_SESSION['myname']? It is a much better way of doing it than putting register_globals On.
     
    jestep, Nov 27, 2006 IP
  5. newlook

    newlook Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I agree that it's a much better/more secure way (again, I wrote the code while a newbie...) -- it's just that I'll have to do a lot of rewriting... :)

    I didn't check the error logs, because it's not really an error thing. (Although I did just check now and... nothing...)

    I may have a workaround; I'll have to test it. After the workaround, I'll have to work on rewriting things, so that it works with register_globals off... I'm self-taught when it comes to PHP, so I'm sure there's bad coding practices in there... :)

    Thanks!
     
    newlook, Nov 27, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Place this at the top of page 2 and see what you get.
    
    
    error_reporting(E_ALL);
    
    PHP:
     
    nico_swd, Nov 27, 2006 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Hmm. I figured it would throw a warning if it couldn't find that variable. It should work the way I was seeing it. I completely understand the re-coding nightmare. Im self-taught myself, and just recently started taking some classes so that I can fill in some gaps. I learned a lot of stuff that I had been doing was not the best way of doing it, and now Im working on re-writing a lot.
     
    jestep, Nov 27, 2006 IP
  8. newlook

    newlook Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @nico_swd: did so; nothing happened.
    @jestep: At least I'm not alone! :)

    I did find a workaround... And then I'll spend some time doing some reading and re-coding...

    Thanks all!
     
    newlook, Nov 27, 2006 IP
  9. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use the backslash character to escape symbols: "... \$_SESSION ..."

    session_register() requires register_globals to operate. It is also deprecated. Assign values directly to the $_SESSION[] superglobal array instead, and read from that.

    With regards to manually setting register_globals to ON, I suspect your host has not enabled this facility in the new php.ini.

    If you want to catch bad coding, use the strict standards setting:
    error_reporting(E_ALL ^ E_STRICT);
    PHP:
     
    penagate, Nov 28, 2006 IP
    newlook likes this.
  10. newlook

    newlook Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks, penagate! I was looking for that sort of confirmation! (And, yes, we can no longer manually set register_globals to ON.)

    Thanks again, to everyone...
     
    newlook, Nov 28, 2006 IP