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
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");



  }

Link to comment
Share on other sites

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");
  }

Link to comment
Share on other sites

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";
}

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.