Jump to content

help with using get


mike1313

Recommended Posts

So on one of my pages I use $_GET to display various contents depending.

 

For ex.

 

<?php
if(!$_GET[per]){
echo "Main Page Content";
}

 

So basically what I want to try and do is say the users clicks on the URL bar and the filename is page.php?per=find. If anyone types in the url bar and changes the find to like page.php?per=hello it would display some of the page but it would be blank mostly. Is there a certain way to stop this or let me display a message. If a $_GET varaible is used but isn't defined in the pages code?

Link to comment
https://forums.phpfreaks.com/topic/82899-help-with-using-get/
Share on other sites

You could use a switch:

<?php
switch($_GET['per'])
{
case 'a':
	// do something
	break;
case 'b':
	// do something else
	break;
default:
	// if nothing else matches
	break;
}
?>

 

Or you could define an array of pages:

<?php
$pages = array('a', 'b', 'home', 'something', 'test');

if(isset($_GET['per']) && !in_array($_GET['per'], $pages))
{
echo 'invalid page';
}
else {
echo 'valid page';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/82899-help-with-using-get/#findComment-421612
Share on other sites

Just use an else statement to echo some result of a non-defined variable.

 

page.php?per=find would result in "Option 1"

page.php?per=hello  would result in "Option 2"

page.php?per=test  would result in "Option 3"

page.php?per=OMG IM HACKING YOUR WEBSITE  would result in "Option 4"

 

<?php
$page = $_GET['per'];

if ($page == "find") {
echo "Option 1";
} elseif ($page == "hello") {
echo "Option 2";
} elseif ($page == "test") {
echo "Option 3";
} else {
echo "Option 4";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/82899-help-with-using-get/#findComment-421614
Share on other sites

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.