Jump to content

Flexible url (example.com/?page=desiredpage)


jhb

Recommended Posts

I have a menu-item that links to for example example.com?page=y

 

Then I need my php code to pull that "y" and put it in "include("pages/'$thatpage'.php");"

 

Got this, that doesn't work:

  if (isset($_GET['page']) && $_GET['page'] == $page) {
 include("pages/'$page'.php");
 }
  else {
  include("pages/index.php");
  }

^ $page should then become the desired page, for example "y"

 

This does though:

  if (isset($_GET['page']) && $_GET['page'] == y) {
 include("pages/y.php");
 }
  else {
  include("pages/index.php");
  }

 

But i need it to be flexible, so i don't have to update my file every time i add a new page.

 

Link to comment
https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/
Share on other sites

How is $page defined? Do you have the code for that. It is *very* risky using variables to include files. For example, if $page is simply a clean equivalent of the $_GET variable..that's not good.

 

On your other question, you would use file_exists():

 

  if (isset($_GET['page']) && $_GET['page'] == $page) {


if(file_exists("pages/$page.php")){
  include("pages/$page.php");
}else{
   //error page or code
}



}
  else {



  include("pages/index.php");



  }

Got this to work:

$page = $_GET['page'];
  if (isset($_GET['page']) && $_GET['page'] == $page) {
  	
 if (file_exists("pages/$page.php")) {
 	include("pages/$page.php");
 	}
 else {
 	echo "<h1>Finner ikke siden</h1>";
 	}

 }
  else {
  include("pages/index.php");
  }

The first line will throw an error if the 'page' index doesn't exist. that is the entire point of the if statement following it.

 

Code should be....

 

if (isset($_GET['page'])) {
  $page = $_GET['page'];  
  if (file_exists("pages/$page.php")) {
    include "pages/$page.php";
  } else {
    echo "<h1>Finner ikke siden</h1>";
  }
} else {
  include "pages/index.php";
}

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.