Jump to content

[SOLVED] Problem using $_GET['page']


Edward

Recommended Posts

 

I am using a variable $_GET['page'] to determine what page should be displayed, by the following code:

 

index.php:

if ($_GET['page'] == '01_01_welcome') {
$page = 'Welcome';
} else if ($_GET['page'] == '01_02_news') {
$page = 'News';
} else if ($_GET['page'] == 'downloads') {
$page = 'Downloads';
} else {
$_GET['page'] = '01_01_welcome';
$page = 'Welcome';
}

 

This way, if a user has not yet chosen a page from the menu, it will default to the Welcome page. This should work fine, but I'm getting an error I've never seen before. Here is the error message I get:

 

Notice: Undefined index: page in e:\domains\i\my-domain.com\user\htdocs\test\index.php on line 18

 

Please can someone tell me how to get around this?

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/
Share on other sites

That's just PHP saying, "Hey!  You're trying to use a non existant variable!"

 

I bet when the url has page in it, that message doesn't show up.

 

An easy way to fix that without having to recode a lot of it would be to add the following at the top:

 

if(!isset($_GET['page'])) $_GET['page'] = '01_01_welcome';

That's great! Thank yo very much. What I previusly used at the start was this:

if (!$_GET['page']) {
			$_GET['page'] = '01_01_welcome';
			$page = 'Welcome';
		}

 

...which I thought should work the same as your suggestion, but it didn't. Your suggestion solved it. Does this vary between PHP versions, as my alternative code worked fine on my local server and on another domain. Either way, thank you very much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.