1

I'm looking to do something really rather simple but I'm quite new to PHP so please pardon my naivety.

My requirement is to have a simple page (here on referenced as base) which will be populated by values from another file (here on referenced as inc). The output HTML from base may look something like this:

    <h2>Name: [dynamic string (e.g. Joe Bloggs) ]</h2>
    <h2>Age: [dynamic value (e.g. 31) ]</h2>
    <h2>Bio: [dynamic string (e.g. I was born and raised in the UK. My special features are rocket boots.)</h2>

I plan on using a URL parameter (e.g. "www.mydomain.com/base.php?id=1234") to identify the name of the inc file that will be used in a get request.

I'm not exactly sure if this is the best way to go about it, but as this is a REALLY simple project I want to avoid using mySQL or anything else to complicate matters in production.

Please can you advise:

  1. How should I construct the inc PHP file? Would it be best to create the variables in this as a form or array?
  2. How can I configure the base PHP to pull in the variables that are set in an external file

Please let me know if my logic is way off & this is completely the wrong way to go about it!

As I mentioned before, this is a very simple project so I would like to stick to raw PHP (no 3rd party tools) as I'm using this to educate myself. Thank you for any help!

2 Answers 2

2

absolute simplest method:

include_data.php:

<?php

$name = 'Joe Bloggs';
$age = 31;
$bio = 'I was born and raised ...';

biopage.php:

<?php

include('include_data.php');

?>

<h2>Name: <?php echo $name ?></h2>
<h2>Age: <?php echo $age ?></h2>
<h2>Bio: <?php echo $bio ?></h2>

That's all you REALLY need to make a "dynamic" page in php. Everything else is just fancier and fancier versions of this.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Marc, thanks for the suggestion! So that I can make this dynamic, would I be correct in thinking I could change "include_data" to be a variable to be set from the URL?
you can, but if you directly allow specifying files to include via url, you open your system to HUGE security vulnerabilities. unless you do it right, a malicious user can get your server to spit out ANY file on your server: e.g. http://example.com/bio.php?inc=../../../../../../../etc/passwd
If I was to just reference the id in the URL (eg www.mydomain.com/file.php?id=1234). and create a location in my PHP file would this alleviate the issue?
1

if your inc files look like:

<?php
$name = "My Name";
$age = 21;
$bio = "This is my bio";

base.php would look something like this:

<?php
$inc = $_GET['id'] . ".inc.php";
include($inc); // or require($inc); if you want to throw an error if $inc doesn't exist
?>
<h2>Name: <?=$name?></h2>
<h2>Age: <?=$age?></h2>
<h2>Bio: <?=$bio?></h2>

you'd want to append whatever subdirectory your inc file's in if it's not in the same directory as base.php. Also you could check if $inc file actually exists and display an error if necessary.

you could have the variables in the inc files in an array, I don't think it makes much of a difference in such a simple example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.