3

Ok so i want to have an include code on my index.php page that will include all of the other html pages within it. I used to do this using an id?=link.html link and this:

<?php 
$id = $HTTP_GET_VARS['id'];
if ( !$id || $id == "" )
 { 
 $number=10;
 include("news/show_news.php");
 } 
else
 { 
 include "$id"; 
} 
?>

but apparantly http_get_vars is unrecognized or something? How can I fix this so that it will work? Or if things have changed, why should I not use this kind of thing for includes?

Oki Erie Rinaldi
  • 1,835
  • 1
  • 22
  • 31
Phate
  • 31
  • 2
  • `include` still working (with my php version, about 5.4.x). `$HTTP_GET_VARS['id']` = it's really old. why don't you use `GET` or `POST` method? so the html : `page.php?id=the_id` php : `$id = $_GET['id']` – Oki Erie Rinaldi Oct 26 '13 at 02:54
  • this code makes security issue, please refer to manuals, books, etc. – Peter Oct 27 '13 at 01:23

2 Answers2

2

You can use $_GET to fetch the query string parameter by GET request, e.g.

index.php?id=123

The id value can be obtained by $_GET['id'].

p.s. your PHP codes are really old.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • this is what ive got now and its not quite working, its saying id is undefined – Phate Oct 26 '13 at 02:47
  • and ya lol these codes are from like 10 years ago, ive just recently been getting back into programming now that html5 is in full swing – Phate Oct 26 '13 at 02:48
  • What is the Request URL ? e.g. `index.php?id=123` ? – Raptor Oct 26 '13 at 02:49
  • 3
    @user2922030 Don't include a user provided file name! It's a security risk. – John V. Oct 26 '13 at 02:51
  • the request url would be any link on the page that begins with ?id= so for example if i clicked the link ?id=contact.html or ?id=about.html, the contents of those pages would go where the include code is on the index.php page. so that on the index.php page it would show my news instead – Phate Oct 26 '13 at 02:52
  • and thanks for the good looks John V :) ill be more careful – Phate Oct 26 '13 at 02:53
  • You sure `about.html` exists in the same path with your request file? change `include` to `require` and turn on error reporting to debug. – Raptor Oct 26 '13 at 02:56
  • about.html was just an example, my goal is to fix it so it will include literally any link that leads with ?id=, of course only one at a time, else include my news stuff – Phate Oct 26 '13 at 02:59
  • suggest to rewrite the logic from the beginning – Raptor Oct 26 '13 at 03:06
  • not sure how to ask this without asking you to write the code for me, but how would i do that? im super rusty on php syntax – Phate Oct 26 '13 at 03:15
0

solved with the following:

        <?php 

        if (!empty($_GET['id'])) { 
        $id = $_GET['id'];  
        $id = basename($id);  
        include("$id");  
        } else {
        include("news/newsfilename.php");
        }

        ?>    

and

             <a href="index.php?id=pagename.html">
             Page Name
             </a>

as the html

Phate
  • 31
  • 2
  • this code makes security issue, please refer to manuals, books, etc. at least please warn users this code is not safe at all – Peter Oct 27 '13 at 01:24