14 Replies - 901 Views - Last Post: 04 December 2011 - 11:03 AM Rate Topic: -----

#1 farril  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 04-March 09

creating a default key

Posted 30 November 2011 - 03:16 PM

Hi,

I've got a few keys (i think thats what you call them) in my web address. At the moment when i miss out a key it doesn't know what to do. How would i set it so it defaults to a certain variable.

For example i have

index.php?id=chest&page=7



if i dont put the page key in i'd want it to choose page=1 automatically. And if i didn't have an id (or had the wrong id, i'd want it to default to another key (for example index.php?error=1) so i could display the appropriate error message

index.php?id=chest

changes to

index.php?id=chest&page=1

and

index.php?page=5 or index.php?id=bananas&page=5

changes to

index.php?error=1



Any ideas how i go about going this? I'm abit stumped.

I tried putting $page = 1; above the $_GET line, but that didn't change anything.

I can provide more of my code if its required.

Thanks,

This post has been edited by farril: 30 November 2011 - 03:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: creating a default key

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: creating a default key

Posted 30 November 2011 - 03:33 PM

you donít necessarily need to change the URL for that. all you need is proper error handling by using theómentionedódefaults.

example
// define defaults
define("DEFAULT_PAGE", 1);

try
{
    // check key
    if (!isset($_GET['id']))
    {
        throw new InvalidArgumentException("ID is missing.");
    }

    // set page
    $page = isset($_GET['page']) ? $_GET['page'] : DEFAULT_PAGE;

    // rest of the code
}
catch (Exception $e)
{
    // error code here
    // e.g. insert a 404 page
}

Was This Post Helpful? 3
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,208
  • Joined: 19-January 10

Re: creating a default key

Posted 30 November 2011 - 03:34 PM

They aren't called keys, however I'm unsure on the exact terminology to use but I've gotten along just fine by calling them variables.

The method to prevent this is simple. There's a built-in function in PHP which lets you know if a variable exists/is set or not. It's called isset.

here's how you would use it:

if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}


This post has been edited by creativecoding: 30 November 2011 - 03:35 PM

Was This Post Helpful? 1
  • +
  • -

#4 farril  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 04-March 09

Re: creating a default key

Posted 30 November 2011 - 04:11 PM

thanks both of you for the very prompt responses. those helped me out tons, will give it a go implementing them tomorrow.
Was This Post Helpful? 0
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,208
  • Joined: 19-January 10

Re: creating a default key

Posted 30 November 2011 - 04:17 PM

View PostDormilich, on 30 November 2011 - 10:33 PM, said:

    $page = isset($_GET['page']) ? $_GET['page'] : DEFAULT_PAGE;



can you explain how this line works?
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: creating a default key

Posted 30 November 2011 - 04:21 PM

@creativecoding - That is commonly referred to as a ternary statement. Basically if is the shorthand for an if/else statement.

This:
$page = isset($_GET['page']) ? $_GET['page'] : DEFAULT_PAGE;



Is equivalent to this:
if(isset($_GET['page']))
  $page = $_GET['page'];
else
  $page = DEFAULT_PAGE;



NOTE - I have not put the curly brackets in the second code example because they aren't needed. But that is the same as this:
if(isset($_GET['page'])){
  $page = $_GET['page'];
}
else{
  $page = DEFAULT_PAGE;
}



NOTE 2 - You can only have boolean statements and a couple values in ternary statements. However, that isn't to say that you can't make things far more complicated than the example above. Readability just starts falling off quickly when you do.
Was This Post Helpful? 2
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: creating a default key

Posted 30 November 2011 - 04:23 PM

It's the same as

if (isset($_GET['page'])) 
    $page = $_GET['page'];
else
    $page = DEFAULT_PAGE;

Was This Post Helpful? 1
  • +
  • -

#8 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,208
  • Joined: 19-January 10

Re: creating a default key

Posted 30 November 2011 - 04:26 PM

I learn something new every day, thanks.

This post has been edited by creativecoding: 30 November 2011 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: creating a default key

Posted 01 December 2011 - 02:01 AM

however omitting curly braces is considered bad coding practice. imagine the following:
if (isset($_GET['page']))
	$page = $_GET['page'];
	doSomethingElse(); // newly inserted line
else
	$page = DEFAULT_PAGE;

what would you expect to happen?
Spoiler

Was This Post Helpful? 1
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: creating a default key

Posted 01 December 2011 - 06:27 AM

I like setting defaults for input variables like this:
($page = @$_GET['page']) or $page = DEFAULT_PAGE;



You can also shorten the syntax of the ternary operator if you are using >= PHP 5.3
$page = (int)@$_GET['page'] ?: DEFAULT_PAGE;


Note that because of the (int), the $_GET['page'] will be converted to an integer before the comparison. Which means that if you pass an invalid integer value (like a string), it would be converted to 0, evaluated as false, and the DEFAULT_PAGE would be used. (Which also means that if you pass 0, the default will be used!)

This post has been edited by Atli: 01 December 2011 - 06:29 AM

Was This Post Helpful? 1
  • +
  • -

#11 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,208
  • Joined: 19-January 10

Re: creating a default key

Posted 01 December 2011 - 09:38 AM

I'm not a fan of @ and instead error_reporting should be turned off in your php.ini file for production releases.
Was This Post Helpful? 0
  • +
  • -

#12 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: creating a default key

Posted 01 December 2011 - 12:39 PM

View PostAtli, on 01 December 2011 - 06:27 AM, said:

I like setting defaults for input variables like this:
($page = @$_GET['page']) or $page = DEFAULT_PAGE;


I tend to use something like this in my code to check for defaulting passed parameters and required fields:
$parameter['defaultToZero']		= (isset($_GET['defaultToZero']))	? $_GET['defaultToZero']	: 0;
$parameter['defaultToValue']		= (isset($_GET['defaultToValue']))	? $_GET['defaultToValue']	: 'Hello World';
$parameter['castAsInt']			= (isset($_GET['castAsInt']))		? (int)$_GET['castAsInt']	: 0;
if(!isset($_GET['requiredField']))	return false;
$parameter['requiredField']		= (!empty($_GET['requiredField']))	? $_GET['requiredField']	: DEFAULT_VALUE;


...which probably looks rubbish in those code tags, but formats nicely in my code editor.


View Postcreativecoding, on 30 November 2011 - 03:34 PM, said:

They aren't called keys, however I'm unsure on the exact terminology to use but I've gotten along just fine by calling them variables.

I believe they are called parameters :)
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: creating a default key

Posted 01 December 2011 - 12:54 PM

while we’re at it, PHP filter functions offer some additional treat, you can check for what you expect and don’t have to test if the URL parameter exists in the first place.

// using the page key for example
// it should be a number and (obviously) larger than 0
$opt = array(
    "options" => array( "min_range" => 1 )
);
$page = filter_input(INPUT_GET, "page", FILTER_VALIDATE_INT, $opt);

// if $_GET['page'] is not defined,  $page => null
// if $_GET['page'] is out of range, $page => false
// otherwise it will be an integer

// set default if needed
if (!is_int($page)) 
{
    $page = DEFAULT_PAGE;
}

This post has been edited by Dormilich: 01 December 2011 - 12:55 PM

Was This Post Helpful? 3
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: creating a default key

Posted 01 December 2011 - 07:59 PM

View Postcreativecoding, on 01 December 2011 - 04:38 PM, said:

I'm not a fan of @ and instead error_reporting should be turned off in your php.ini file for production releases.

True, but in cases where you can predict and properly handle the warning it is silencing there is no harm in using it, if only so PHP wont bug you about it during the development process, or if you don't want the warning to show in the error logs once on the production servers.
Was This Post Helpful? 0
  • +
  • -

#15 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Re: creating a default key

Posted 04 December 2011 - 11:03 AM

You should really just omit the @ and not modify upon release but rather handle everything. Within the page model it is a good idea to check against an array of all url values to check that they are there. If they are not then you can throw a custom user error and log the sucker. This will ensure that you are not going to be missing a value and the script is not going to be run by someone that may be trying to pass in their own values.

This post has been edited by calebjonasson: 04 December 2011 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1