...

View Full Version : Blocking access or "die" action, when id in url is equal to X...



tomystein
02-29-2012, 11:48 AM
My URL looks like this:
http://www.example.com/example.php?action=edit&id=35

I want to add to example.php a command that will block access to the page or will have a "die" command, when id = 35 in the URL.

When it is a different id, the page stays the same as it was.

I thought of using $_GET['id']



<?php
$id = $_GET['id']

if ($id == "35") {
die;
} else {
// do nothing
}
?>


Can you suggest?

djm0219
02-29-2012, 01:13 PM
Did you try that? If you did did it not do what you wanted?

tomystein
02-29-2012, 02:09 PM
I didn't try. I wanted to know if that's alright and if you think it is the best way to do what I want in the first place - blocking access or just not running the rest of the code ("die")

tomystein
02-29-2012, 02:10 PM
is it written correctly?

Fou-Lu
02-29-2012, 02:56 PM
No its not, you have a syntactical error and you haven't checked for if the get actually exists. I assume if no get exists that it will continue as normal.
If you are using hard coded values, I'd use an in_array instead. That will make it easier to add things if necessary.


<?php

$aDie = array(35);

if (isset($_GET['id']) && in_array($_GET['id'], $aDie))
{
die('invalid id');
}

// process stuff.

tomystein
03-04-2012, 09:38 PM
Thanks for the info - it works.
A simple question: How do I write, all IDs smaller than 35?

I want to do "invalid id" when it's smaller than X ID.

Fou-Lu
03-05-2012, 01:05 AM
Thanks for the info - it works.
A simple question: How do I write, all IDs smaller than 35?

I want to do "invalid id" when it's smaller than X ID.

That would require an if instead of an in_array.


if ($value <= 35)
{
# its less than or equal to 35
}

tomystein
03-05-2012, 08:15 AM
Can you help me and write the new code please? I am a bit confused..

Thank you.

tomystein
03-05-2012, 01:55 PM
<?php

$value = '35';

if (isset($_GET['id']) && ($_GET['id'], $value <= 35))
{
die('invalid id');
}

// process stuff.


??

Fou-Lu
03-05-2012, 02:26 PM
No, that would be:


if (isset($_GET['id']) && $_GET['id'] <= 35)

tomystein
03-07-2012, 09:30 AM
Thanks a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum