Jump to content

Php Url parameter


adamriley

Recommended Posts

Hi my php code is

 

--------------------------------------------------------------

<?php

if (isset($_GET['a']) && $_GET['a'] != "") {

if (isset($_GET['b']) && $_GET['b'] != "") {

$a = $_GET['a'];

$b = $_GET['b'];

if (file_exists('pages/'.$a.'.$b')) {

@include ('pages/'.$a.'.$b');

} elseif (!file_exists('pages/'.$a.'.$b')) {

echo 'Page you are requesting doesnt exist';

}

} else {

@include ('pages/1.php');

}

?>

---------------------------------------------

my url is "sitemap.php?a=1&b=php"

--------------------------------------------

 

could you say why its not working im a beginner

Link to comment
Share on other sites

you are missing a } at the end of your script..

formatting helps find these things out :)

<?php
if (isset($_GET['a']) && $_GET['a'] != "") {
if (isset($_GET['b']) && $_GET['b'] != "") {
	$a = $_GET['a'];
	$b = $_GET['b'];
	if (file_exists('pages/'.$a.'.$b')) {
		@include ('pages/'.$a.'.$b');
	} elseif (!file_exists('pages/'.$a.'.$b')) {
		echo 'Page you are requesting doesnt exist';
	}
} else {
	@include ('pages/1.php');
}
}
?>

Link to comment
Share on other sites

This part of the script is now solved but now it seems that it cannot find the page when i try the script with 1 parameter. The script excludes the "Page you are requesting doesnt exist"  :confused:

 

Sorry that one is very confusing what i ment is that bit of the script now works but the script now says "Page you are requesting doesnt exist"

Link to comment
Share on other sites

You need to simplify the code down a bit..

if (file_exists('pages/'.$a.'.$b')) {
         @include ('pages/'.$a.'.$b');
      } elseif (!file_exists('pages/'.$a.'.$b')) {
         echo 'Page you are requesting doesnt exist';
      }

can be simply put as

if (file_exists('pages/'.$a.'.$b')) {
         @include ('pages/'.$a.'.$b');
      } else {
         echo 'Page you are requesting doesnt exist';
      }

because you already know the page doesnt exist...

 

Here is a cleaner version..

<?php
if (isset($_GET['a']) && isset($_GET['b']) && $_GET['a'] != '' && $_GET['b'] != '') {
$a = $_GET['a'];
$b = $_GET['b'];
if (file_exists('pages/'.$a.'.$b')) {
	include('pages/'.$a.'.$b');
} else {
	echo 'Page you are requesting doesnt exist';
}
} else {
include ('pages/1.php');
}
?>

Make sure that your directory structure matches what you are testing for..

 

Link to comment
Share on other sites

You could infact ignore that file_exists..

<?php
if (isset($_GET['a']) && isset($_GET['b']) && $_GET['a'] != '' && $_GET['b'] != '') {
   $a = $_GET['a'];
   $b = $_GET['b'];
   if (!@include('pages/'.$a.'.'.$b)) {
     echo 'Page you are requesting doesnt exist';
   }
} else {
   include ('pages/1.php');
}
?>

 

Edit:

I just noticed an error which is gone in the code above

'pages/'.$a.'.$b'

would be looking for 1.$b

Link to comment
Share on other sites

Did you use the last edited version I posted?

<?php
if (isset($_GET['a']) && isset($_GET['b']) && $_GET['a'] != '' && $_GET['b'] != '') {
   $a = $_GET['a'];
   $b = $_GET['b'];
   if (!@include('pages/'.$a.'.'.$b)) {
     echo 'Page you are requesting doesnt exist';
   }
} else {
   include ('pages/1.php');
}
?>

Link to comment
Share on other sites

then it would be

<?php
if (isset($_GET['a']) && isset($_GET['b']) && isset($_GET['c']) && $_GET['c'] !='' && $_GET['a'] != '' && $_GET['b'] != '') {
   $a = $_GET['a'];
   $b = $_GET['b'];
   if (!@include($c.'/'.$a.'.'.$b)) {
     echo 'Page you are requesting doesnt exist';
   }
} else {
   include ('pages/1.php');
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.