Jump to content

PHP if.. else / url


rswildy

Recommended Posts

How would i go about making an if else statement that will do the following.If user is on page index.php it will redirect user to index.php?id=1 but if they are on index.php?id=whatever the redirection script will have no affect.Thanx, Rswildy.

Link to comment
Share on other sites

Hi!Take a look at this:

<?phpif (!isset($_GET['id'])) {	 header( 'Location: index.php?id=1' );		 exit();}?>

That should work fine, if you use forms and have a server/host that don't support POST and GET on the same time, you can use this

<?phpif (!isset($_REQUEST['id'])) {	 header( 'Location: index.php?id=1' );		 exit();}?>

Hope that helped..Good Luck and Don't Panic!

Link to comment
Share on other sites

Here's an alternative approach I prefer using:

<?php$id = 1;if(isset($_GET['id'])) {$id = $_GET['id'];}//whatever code you want to use?>

That way, if the ID variable is not set, the script will continue with 1 as the default value. It will otherwise use the assigned ID. It seems more efficient, as the browser won't make a second request.For extra security, you might also consider:

<?php$id = 1;if(ereg('^[1-9][0-9]*$',$_GET['id])) {//Ensures ID is set AND that it's actually a positive integer value.$id = $_GET['id'];}//whatever code you want to use?>

Here, the new value won't be used if ID is not a number, and the script will continue with 1 as it's value.

Link to comment
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...