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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-982478
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-982490
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-982493
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-982519
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-982526
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
https://forums.phpfreaks.com/topic/186041-php-url-parameter/#findComment-983214
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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