SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 38

Thread: $php_self

  1. #1
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $php_self

    Hi,

    I searched for &_SERVER and $PHP_SELF without any luck. So, though I'm sure it's been covered before, I'm going to ask anyway....

    I'm working through Kevin Yank's book "Build your own database driven website Using PHP & MySQL" and I've come to a bit of a snag in chapter 3.

    The code provided in the book for a "Multi-Purpose Page Outline" is as such:

    (I see that VB3 doesn't like Mozilla 1.3 on Linux... it won't let me use the php code tags, so I'll keep it brief)


    <?php if ( !isset($name) ): ?>

    <!-- no name, so form for getting name -->

    <form action="<?=echo($PHP_SELF)?>" method="get">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="Go" />
    </form>

    <?php else: ?>

    <p>Your name: <?=$name?></p>
    <p>This paragraph contains a <a href="newpage.php?name=<?=urlencode($name)?>">link</a> blah..
    </p>

    <?php endif; ?>


    Now, I'm an absolute newbie as far as PHP and SQL, so forgive me if this is just a simple thing, which I'm sure it is. But, to my understanding of the book, if there is no name given, then the script displays a form to enter a name with a submit button. That much works. However, when I supply a name, I get a 404 error, saying that the requested URL /< was not found.

    I should say that I'm running Red Hat 8, and just have a default Apache 2.0 server running with PHP installed.

    I'm not sure if I'm missing something, or if there was a misprint, or what. Please help, I'm anxious to learn as much as I can, and it's hard to not get discouraged when problems crop up so early in the game.

    -Rahven

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_SERVER['PHP_SELF'] is the proper usage.
    Aaron Brazell
    Technosailor



  3. #3
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is probably due to the fact that you're running a newer version of PHP which has register_globals set to off. to quote http://www.php.net/manual/en/languag...predefined.php

    In PHP 4.2.0 and later, the default value for the PHP directive register_globals is off. This is a major change in PHP. Having register_globals off affects the set of predefined variables available in the global scope. For example, to get DOCUMENT_ROOT you'll use $_SERVER['DOCUMENT_ROOT'] instead of $DOCUMENT_ROOT, or $_GET['id'] from the URL http://www.example.com/test.php?id=3 instead of $id, or $_ENV['HOME'] instead of $HOME.
    so, in your case, you need to change all occurences of $name to $_GET['name'], and $PHP_SELF to $_SERVER['PHP_SELF']
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  4. #4
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by redux
    this is probably due to the fact that you're running a newer version of PHP which has register_globals set to off. to quote http://www.php.net/manual/en/languag...predefined.php



    so, in your case, you need to change all occurences of $name to $_GET['name'], and $PHP_SELF to $_SERVER['PHP_SELF']

    I ran a phpinfo script, and yes, register_globals is off, so then I changed all occurences of $name and $PHP_SELF as you suggested, resulting in the following code:


    <?php if ( !isset($_GET['name']) ): ?>

    <!-- no name, so form for getting name -->

    <form action="<?=$_SERVER['PHP_SELF']?>" method="get">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="Go" />
    </form>

    <?php else: ?>

    <p>Your name: <?=$name?></p>
    <p>This paragraph contains a <a href="newpage.php?name=<?=urlencode($_GET['name'])?>">link</a> blah..
    </p>

    <?php endif; ?>


    However, the problem remains. When I input text for a name and click go, I'm still getting "404 error, the URL /< could not be found on this server." At this point I'm thinking it's a good thing I shave my head, or else I'd be pulling my hair out. Is there something I'm missing?

    I really appreciate your help, and yout tolerance for newbies

    -Rahven

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've missed out the semicolon ';' at the end of the echo command

    -Sam
    Sam Hastings

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    CA
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what echo statment?

    when you run that script and you have the form in front of you
    do 'view source' and look at the action page for the form.

    is it what you expect it to be?

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Is your PHP set to accept short_open_tag? (i.e. <? ). Check your phpinfo().
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    Is your PHP set to accept short_open_tag? (i.e. <? ). Check your phpinfo().

    And we have a winner! That was the problem, though register_globals was off as well. I modified php.ini and turned register_globals and short_open_tag to on, after those modifications, and changing everything back to $name instead of $_GET['name'] it works.

    I turned globals on because that's how my host has their PHP configured.

    I just want to say thank you to all of you. It can be exceptionally frustrating trying to work out why something doesn't work, especially when dealing with a new language. I had moved on to the next chapter and had planned on coming back to that after a break from it, I saw that there were a couple more responses posted, I checked them out, n now everything's working.

    You guys are great, thanks again.

    -Rahven

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rahven
    I modified php.ini and turned register_globals and short_open_tag to on
    noo!


    Quote Originally Posted by Rahven
    changing everything back to $name instead of $_GET['name'] it works.
    noo! why would you do that? $_GET['name'] is the proper way. you're going to have to do it eventually. i've never used register_globals in almost 2 years since starting PHP. if everyone did the same, things would be a lot better.


    Quote Originally Posted by Rahven
    I turned globals on because that's how my host has their PHP configured.
    noo!
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using register_globals isn't an extremely bad thing as long as you know what you are doing, PHP didn't need to depricate it IMO for PHP5 anyways, it's the coders fault if he doesn't know how to do it properly, not PHP's.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only reason I did it, was because my site host has theirs configured with register_globals set to on. Because I'm starting out with PHP, I thought that, until I get more proficient in the language, that it would be in my best interest to keep things configured like my hosts. After I become more familiar with the language, and more confident in my abilities, then I'm sure I'll be able to wrok around any problems that arise, but since I'm just developing for myself at the moment, I'm trying to keep the learning curve down, as well as frustration. There is nothing so disheartening as spending an entire day scripting something and having it work just fine on your own computer, only to find out that it's only good for generating errors on your hosts computer.

    I'm sure that the biggest problem was that short_open_tags was set to off. Will $_SERVER['PHP_SELF'] still work with register_globals on?

    -Rahven

  12. #12
    SitePoint Member
    Join Date
    Mar 2003
    Location
    london
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh this was what i was going to postT! well unfortunatly i posted reply saying ill try it but i have a different error! my short_open thing is on and globals is off! basically when i run the script i have:

    Your name:
    This paragraph contains a link that passes the name variable on to the next document.
    "box with submit button"

    all showing. dunno if thats how its meant to be. when i run the script with any text i get this url after with same page again and name not picked up. http://localhost/%3c/?name=test
    i am using apache and learning on my own server. the strange thing is apart from not picking up name when i first click link itll take me to the other page but with no name obviously..if i run the script and get that longer url with /%3c/ added each time thereafeter the link doesnt work and i get internal server error page.? anyone so kind to hlep me too ? cheers [img]images/smilies/rolleyes.gif[/img]

    <?php if ( isset($_GET[name]) ): ?>

    <P>Your name: <?php echo($_GET[name]); ?></P>

    <P>This paragraph contains a
    <A HREF="newpage.php?name=<?php echo(urlencode($_GET[name])); ?>">link</A> that passes the
    name variable on to the next document.</P>

    <?php else: ?>

    <FORM ACTION=<?php "&_SERVER[PHP_SELF]"; ?> <METHOD=GET>
    Please enter your name: <INPUT TYPE=TEXT NAME="name">
    <INPUT TYPE=SUBMIT VALUE="GO">
    </FORM>

    <?php endif; ?>

    thats the important bits!
    Last edited by NeoMatrix; Mar 29, 2003 at 13:50.

  13. #13
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    Using register_globals isn't an extremely bad thing as long as you know what you are doing, PHP didn't need to depricate it IMO for PHP5 anyways
    yeah, they should remove register_globals from PHP 5 completely with no option to turn it on. ever.

  14. #14
    SitePoint Member
    Join Date
    Mar 2003
    Location
    london
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help for me guys? please


  15. #15
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code:
    PHP Code:
    <?php if ( isset($_GET[name]) ): ?>

    <P>Your name: <?php echo($_GET[name]); ?></P>

    <P>This paragraph contains a
    <A HREF="newpage.php?name=<?php echo(urlencode($_GET[name])); ?>">link</A> that passes the
    name variable on to the next document.</P>

    <?php else: ?>

    <FORM ACTION=<?php "&_SERVER[PHP_SELF]"?> <METHOD=GET>
    Please enter your name: <INPUT TYPE=TEXT NAME="name">
    <INPUT TYPE=SUBMIT VALUE="GO">
    </FORM>

    <?php endif; ?>
    Correct code:
    PHP Code:
    <?php if ( isset($_GET['name']) ): ?>

    <P>Your name: <?php echo($_GET['name']); ?></P>

    <P>This paragraph contains a
    <A HREF="newpage.php?name=<?php echo(urlencode($_GET['name'])); ?>">link</A> that passes the
    name variable on to the next document.</P>

    <?php else: ?>

    <FORM ACTION=<?php "&_SERVER['PHP_SELF']"?> <METHOD=GET>
    Please enter your name: <INPUT TYPE=TEXT NAME="name">
    <INPUT TYPE=SUBMIT VALUE="GO">
    </FORM>

    <?php endif; ?>

    Basically you forgot to single-quote the variable names in the brackets.

    -Rahven

  16. #16
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not quite. i'd use something like this:

    PHP Code:
    <?php if (isset($_GET['name'])) { ?>

    <p>Your name: <?php echo($_GET['name']); ?></p>

    <p>This paragraph contains a
    <a href="<?php echo $_SERVER['PHP_SELF'], '?name='urlencode($_GET['name']); ?>">link</a> that passes the
    name variable on to the next document.</a>

    <?php } else { ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="GO" />
    </form>

    <?php ?>

  17. #17
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh, got me there. To keep it XHTML valid, yours is right. Since NeoMatrix is working from the same book as I am, I was just showing him that his biggest problem was missing the quotes. Just copied and pasted, rather than type it all out myself.

    -Rahven

  18. #18
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just did the XHTML as a nice touch. (as well as not using the if (): ... endif; syntax )

    but i don't think forgetting "to single-quote the variable names in the brackets" is what's causing the problem. he didn't have it the proper way, but it should work. like this line was totally wrong

    PHP Code:
    <FORM ACTION=<?php "&_SERVER['PHP_SELF']"?> <METHOD=GET>

  19. #19
    SitePoint Enthusiast Rahven's Avatar
    Join Date
    Mar 2003
    Location
    In the barrel with the other monkeys.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, ahh the danger of n00bs helping n00bs

  20. #20
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    not quite. i'd use something like this:

    PHP Code:
    <?php if (isset($_GET['name'])) { ?>

    <p>Your name: <?php echo($_GET['name']); ?></p>

    <p>This paragraph contains a
    <a href="<?php echo $_SERVER['PHP_SELF'], '?name='urlencode($_GET['name']); ?>">link</a> that passes the
    name variable on to the next document.</a>

    <?php } else { ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="GO" />
    </form>

    <?php ?>
    Just wondering, is there a difference between:

    <?php echo $_SERVER['PHP_SELF'], '?name=', urlencode($_GET['name']); ?>

    and:

    <?php echo $_SERVER['PHP_SELF'] . '?name=' . urlencode($_GET['name']); ?>

    That would affect the outcome?
    Mike
    It's not who I am underneath, but what I do that defines me.

  21. #21
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not affect the result, no. i just use commas to seperate multiple parameters to echo (it saves 1 byte by not putting a space if front of the , like with . ) since it's supposed to be faster than concatenating multiple strings with . and i think it looks neater.

    note: you can't use commas with echo if you use parentheses: echo('foo', 'bar') won't work.

  22. #22
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, good to know
    Mike
    It's not who I am underneath, but what I do that defines me.

  23. #23
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not just not put a space in front of the . then?
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  24. #24
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    why not just not put a space in front of the . then?
    i knew somebody would say that. that doesn't look "right" though. i don't do it anywhere else, and i never put spaces in front of commas in function parameters, etc.

  25. #25
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you and your "methods"

    alrighty then... i'll respect that
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •