Jump to content
xisto Community
apple

Dealing With Links Like ?id=123 pls help in a small issue

Recommended Posts

While dealing with such a links in php

page.php?id=1
i dont want that someone access this page directly using page.php..
i can give some custom message incase someone try to access page like:
page.php
okay i can make it by using if condition etc..
but the other problem is that, if someone try to access like,
page.php?id=
in this case (where id is not given, but there's only "?id= " which makes no sense.. in that case i want to make some custom msg etc. how i can handle this situation?
Regards

Share this post


Link to post
Share on other sites

maybe u should try using .htacces, for redirecting page.php to another page. i really don't know proper code to do that but someone else here might know (i'm also interested in that .htaccess code :P )

Share this post


Link to post
Share on other sites

One method is to define an array of suitable values.
In the following snippet, I have defined an array which I use on a small site.

$data_array = array('index', 'one', 'two', 'three', 'four', 'five', 'contact');

And then I use the following block of code to make sure the "?page=" contains one of those array elements or they get redirected to the index.php page.
Your example uses "?id=", so you will need to modify that. And also, this code block needs some further security added to it, and as you can tell, I include files with ".txt" file extensions. (Included files always parse as html. Add the php tags inside them if they contain php code) The file extensions will need to be altered to the file extension you use.
Small changes, really.

$submit = $_GET

;echo  $_GET

;												 // echo to confirm the value as a test onlyif( !isset($_GET

)   ) {							// page is not set , use first array element				if (file_exists($data_array[0] . '.txt' )) {				include ( $data_array[0] . '.txt' );				}  				else {					include ('index.txt');			   // or default to index page if first file doesn't exist				}		}		elseif (in_array($submit , $data_array)) {	   // value is in array 				if (file_exists($submit . '.txt' )) {	// and exists				include ( $submit . '.txt' );				}				else {					include ('index.txt');			  // default to index page				}		}		else {																	 // value is not in array				if (file_exists($data_array[0] . '.txt' )) {	// and first array name exists				include ( $data_array[0] . '.txt' );				}				else {					include ('index.txt');					 // or use index page				}		}

If you follow the script logic, the default page which is included is the index page. Other values must be contained in the defined array.
As you add pages, simply add an element to the array defined above.

There are other methods to use, just thought you might like to see this one and I had it handy. :P
Hope this helps.

Share this post


Link to post
Share on other sites

When checking the variable ID, are you using the isset function or are you checking if the string == ""?Using string=="" should work if ?id= is enterned and no number.

Share this post


Link to post
Share on other sites

this is the correct code

 

<?php $variable = $_GET['id'];switch($variable) {default: include('home.php'); break;case "forum": include('forum.php'); break;}?>
instructions:

case "forum": include([u]'forum.php'[/u]); break;
= change - forum.php to the url

 

forum - change to your id (underlined) which will come in

index.php?id=[u]forum[/u]

$variable = $_GET['id'];
-change id to what you want here (underlined) index.php?id=123
Edited by mahirharoon (see edit history)

Share this post


Link to post
Share on other sites

If it can't find a case to match the value in the variable, it will display everything in the default section. So, with the example above, if $variable is not equal to forum then the default case is displayed.Personally I would put the default case last. I'm not sure if it makes a difference but it seems to be pretty much standard, unless you are using the fall-through method.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.