# Php problem with undefined variables?

**URL:** https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426
**Category:** Uncategorized
**Created:** [June 17, 2004, 7:37pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426 "2004-06-17T19:37:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Flashmatazz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/flashmatazz/32/140_2.png) [@Flashmatazz](https://forum.kirupa.com/u/Flashmatazz)
#### Post date: [June 17, 2004, 7:37pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/1 "2004-06-17T19:37:39Z")

</div>

I’m playing around with a PHP script and am running into an odd problem.

I’m trying to adapt the RSSViewer wrtiiten by [Erational](http://www.erational.org/software/rssViewer/info.html)

Flash calls the PHP script with an url query

In the PHP script there’s the following:

```php

$category = $_GET['category'];
$id = $_GET['id'];
$action = $_GET['action'];

```

However, when the PHP script is called like this

```php

rssView.php?action=viewCat

```

then I get errors saying that there’s an  
_undefined index: category_ and  
_undefined index: id_

As I understand it the script tries to assign values to these variables that simply are not there 'cause they haven’t been sent to the script.  
How can I prevent this from happening?

Thanks

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 8:32pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/2 "2004-06-17T20:32:18Z")

</div>

nevermind, figured it out already.

I just used

```php

if (isset($_GET['id'])){
  $id = $_GET['id'];
  // do other stuff
}

```

so the variable only gets defined when there’s really a GET variable

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 8:54pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/3 "2004-06-17T20:54:06Z")

</div>

You actually don’t need that on most servers, you could just use $id if global variables are enabled (i think global variables is what it’s called at least)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 9:04pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/4 "2004-06-17T21:04:08Z")

</div>

Global variables are bad for your health. Turn them off right now! They can easily lead to security holes in your code. If you - for instance - have code like this

```php

if (input_is_okay()) {
    $valid_input = 1 ;
}
if ($valid_input) {
    show_financial_data();
}
else {
    show_order_form();
}

```

Then if the script is called like this **script.php?valid\_input=1** then the code will execute the show\_financial\_data routine even though input is _not_ OK.

The code is much more secure from this kind of exploit if you have to access variables through the $\_GET and $\_POST arrays.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 9:47pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/5 "2004-06-17T21:47:43Z")

</div>

I think any version of php after 4.2 comes with register globals turned off.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 9:59pm UTC](https://forum.kirupa.com/t/php-problem-with-undefined-variables/113426/6 "2004-06-17T21:59:56Z")

</div>

But a lot of hosts turn it on as software, for instance osCommerce is a good example, still needs it.

But as was mentioned, just get the idea of using them out of your head. 😛

You can leave global variables on as long as you don’t use them. Use $\_GET, $\_POST, $\_SESSION, etc… instead.
