Click to See Complete Forum and Search --> : php script help...


aaronking
November 19th, 2008, 11:43 PM
Hello,

I have the following php script:

test.php:

<?
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.

PeejAvery
November 20th, 2008, 08:17 AM
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
if (isset($name)) {echo $name;}
else {echo "No Page Name";}
?>

Nightwolf629
November 20th, 2008, 08:26 AM
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.


<?php
if (isset($_GET['name'])){
$name = $_GET['name'];
echo $name;
}else{
echo "No Page Name";
}
?>

aaronking
November 20th, 2008, 09:10 PM
Thanks, I ended up using:


<?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.

Teranoz
November 29th, 2008, 02:06 PM
I assume you might do more than just displaying one line of text with the variable $name.
So better start with

$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.