0

I am trying to setup a website using PHP, and I am relatively new to PHP. I want to be able to make pages that are formatted something like this: http://example.com/index.php?p=pagename where the argument /?p is page and the argument =pagename is what the name of the page is.

I have tried a method of using: <?php include('page.php') ?> but that does not produce the result I want. That makes another page appear in the same page.

Here is what I have tried:

<?php include('page.php') ?>

I also tried:

<?php echo file_get_contents("html/header1.html"); ?>
<title>Cool Page</title>
<?php echo file_get_contents("html/header2.html"); ?>
This is page 1, the home page.
<?php echo file_get_contents("html/footer.html"); ?>

and

<?php include('html/header1.html'); ?>
<title>Cool Page</title>
<?php include('html/header2.html'); ?>
This is page 1, the home page.
<?php include('html/footer.html'); ?>

What I expect is when I want to be able to type in http://example.com/index.php?p=coolpage to get a page called "Cool Page" (if available!) but I am unable to know how that works.

Thanks for any help given!

TheCrafters001
  • 327
  • 2
  • 15
  • You need a GET method for this. – Funk Forty Niner Dec 22 '18 at 14:35
  • @FunkFortyNiner, Could you explain? I am still learning PHP, and currently, my knowledge of it is very limited. – TheCrafters001 Dec 22 '18 at 14:37
  • See https://stackoverflow.com/q/5884807/1415724 and https://stackoverflow.com/q/11480763/1415724 for a few examples. – Funk Forty Niner Dec 22 '18 at 14:44
  • @FunkFortyNiner Now I got the code ``````` but that seems to print out whatever is typed. For example: if I type ```?link=page``` the result is the page, and I get the text saying "page". – TheCrafters001 Dec 22 '18 at 14:51
  • You should consider an MVCE approach to URLs. Meaning, [routing](http://altorouter.com/). You can dynamically map routes this way and allows for pretty urls. IE: `example.com/coolPage` would just be `->map('GET|POST', '/coolPage', function() { .... });` – Jaquarh Dec 22 '18 at 15:00
  • @FunkFortyNiner Using AltoRouter seems to throw out at 500 Error for the type of website I am working on. Is there any other way to get this working or no? If no, I will just make the pages link to each other. – TheCrafters001 Dec 22 '18 at 15:09
  • Yes but why would you, I would rather use a standard MVC URI router. – ArtisticPhoenix Dec 22 '18 at 16:06
  • Examples of good PHP MVC frameworks are Laravel, Symfony, etc. And If you just like a plain vanilla website go for Wordpress, Drupal, Etc. If you like to reinvent the wheel then I would definitely recommend a good book. Remember that for production you would also need to take care of your security. – Mr. Radical Dec 22 '18 at 16:31
  • @Mr.Radical I am just going to stick to the way I normally do it. But thanks for the tips. Also my website does have a SSL, so I think security is fine. – TheCrafters001 Dec 22 '18 at 17:08
  • What I mean with security is code injection. You need to sanitize the input which comes from the GET method. Especially relevant if you are using the input to query a database. – Mr. Radical Dec 22 '18 at 17:10
  • 500 is a server error. Check your logs and enable error reporting http://php.net/manual/en/function.error-reporting.php. – Funk Forty Niner Dec 22 '18 at 17:31
  • You could also consider using .htaccess modrewrite to allow request to specific URLs to point to their original location. Can be much simpler than doing this within the script itself. – Chris J Jan 05 '19 at 18:36

3 Answers3

2

There are 2 big issues here. The first is that your code makes no attempt to read the data supplied in the URL, the second is that you are creating a very insecure application.

PHP will parse the request from the browser and populate the data in at least one of the superglobal variables. Specifically, paramters in the query part of the URL are added to $_REQUEST and $_GET. Hence:

<?php

include($_GET['p'] . ".php");

will appear to function as you request, displaying the output of coolpage.php from the same directory where this code resides. However the code above allows anyone to run any code they want on your server (by default include/require can read from http[s] locations as well as the local filesystem). The following is a lot safer - it restricts the execution to a specific directory:

<?php

$fname=$_SERVER['DOCUMENT_ROOT'] . "/pages/" . basename($_GET['p']) . ".php";
include($fname);
symcbean
  • 47,736
  • 6
  • 59
  • 94
0

You are trying to create a dynamic website where the GET parameter controls what contents it shown to the user, here is how that can be done:

<?php if( in_array($_GET['P'],['home','coolpage']) ): ?>
<?php include('header1.html'); ?>

<title>Cool Page</title>
<?php include('header2.html'); ?>
<?php endif; ?>

<?php if($_GET['P']=='home'): ?>
This is page 1, the home page.
<?php elseif($_GET['P']=='coolpage'): ?>
Foo page
<?php endif; ?>

<?php if( in_array($_GET['P'],['home','coolpage']) ): ?>
<?php include('footer.html'); ?>
<?php endif; ?>

/?P=home outputs:

header1.html
<title>Cool Page</title>
header2.html
This is page 1, the home page.

footer.html

/?P=foo outputs:

header1.html
<title>Cool Page</title>
header2.html
Foo page

footer.html

/?P=IAmNotAPage outputs:

(only a few newlines are output)

This solution works fine for small projects, not recommended for larger projects though as it easily ends up in mezzy spaghetti code.

The application is perfectly safe if you design it as shown where the path to external files are hardcoded. Just don't use environment variables when including files (bad code: <?php include("html/{$_GET['P']}.html");) because it will leave the application open to attacks (as pointed out by symcbean).

This answer provides a secure solution for your question, consider making this the accepted answer.

Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35
-2

It seems that you are asking how to dynamically display a page based on a URL parameter.

PHP Script

The script takes the URL parameter using the GET method, assigns the parameter to a file path, then displays the file using the set path.

The path look at stored pages inside an includes folder. If the file is not found then a new one is created with the text "There are 32 bytes in this file!".

<?php
$including_url = "includes/" . basename($_GET["p"], ".php") . ".php";

if (file_exists($including_url) {
    require $including_url;
} else {
    $new_file = fopen($including_url, "w") or die("nonexistent file could not be created");
    fwrite($new_file, "There are 32 bytes in this file!");
    fclose($new_file);
    require $including_url
}
?>