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
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';

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.