CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2006
    Posts
    228

    php script help...

    Hello,

    I have the following php script:

    test.php:
    Code:
    <?
    if ($name) {
      echo $name;
    }else{
      echo "No Page Name";
    }
    ?>
    and if you goto mysite.com/test.php?name=hello

    it should display the text 'hello' on the page where the script is put but it don't work.

    does anyone know why this is not working for me?

    When i run it, it comes up with 'No Page Name' rather then what I put in the URL

    My web server is:
    Apache version: 2.2.6 (Unix)
    PHP version: 5.2.5

    Hope someone can help

    Thanks.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: php script help...

    You haven't shown how you are getting the $name variable, so we can't tell you anything concerning that. However, when checking for variables, you would use isset(). PHP isn't like most other languages when checking variables.

    PHP Code:
    <?php
    if (isset($name)) {echo $name;}
    else {echo 
    "No Page Name";}
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: php script help...

    the URL does not set the variable itself. You need to pull it out of the $_POST or $_GET array first.

    In this case you are using GET.

    Code:
    <?php
    if (isset($_GET['name'])){
    $name = $_GET['name'];
    echo $name;
    }else{
    echo "No Page Name";
    }
    ?> 
    Last edited by Nightwolf629; November 20th, 2008 at 09:29 AM.

  4. #4
    Join Date
    Mar 2006
    Posts
    228

    Re: php script help...

    Thanks, I ended up using:

    Code:
    <?php
    if (isset($_GET['name'])){
    $name = $_GET['name'];
    echo $name;
    }else{
    echo "No Page Name";
    }
    ?>
    I thought i was missing something in the code.

    Thanks.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    106

    Re: php script help...

    I assume you might do more than just displaying one line of text with the variable $name.
    So better start with
    Code:
    $name=isset($_GET["name"])?$_GET["name"]:"No name";
    echo $name;
    There is a way you can do it the way of your first code, with setting register_globals on, but this is a major security risk to use.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured