Forum Moderators: coopster

Message Too Old, No Replies

Switching content with php variables

         

Light_Gear

8:26 pm on Oct 30, 2006 (gmt 0)

10+ Year Member



Ok I feel really stupid right at the moment.
What I am trying to do is have one index file that contains my head and my footer.

Then for the main content <?php @ require_once ("$page.html";?>

then for the url index.php?page=about

Now the thing I was reading said this --->

<removed copy paste from other site - jatar_k>

What I really want to do is have a the content change on one index file so I do not have to copy php code throughout the website and it would seem that this would be one way to do it.

I guess I could just not be thinking in the right direction and well if anyone can point me in the right way that would be great

[edited by: jatar_k at 4:56 pm (utc) on Oct. 31, 2006]

StupidScript

10:15 pm on Oct 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When your URI is:
index.php?page=about

and you want to load
about.html
, you'll need to grab the GET array variable "page" and then use it.

Running PHP pre-v.4.2 made this easy, as

register_globals
was enabled by default. PHP v.4.2+ disables
register_globals
(wisely) by default, so you need to access the array directly:
$page=$_GET["page"];

... then ...
<?php @ require_once ("$page.html";?>

needs to be fixed ... you're missing a closing parenthesis:
<?php @ require_once ("$page.html"[b])[/b];?>

Should be all there is to it.

(It looks like the "@" sign is automagically grabbing the variables from the URI, although I can't find anything about it in the docs, so accessing the GET array may not be necessary ... in which case the missing paren should fix it up for ya.)

[edited by: StupidScript at 10:18 pm (utc) on Oct. 30, 2006]

mcibor

10:35 pm on Oct 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would also add some validation before you redirect:

$page=$_GET["page"];

$arr_allowed_pages = array("about", "history", "contact");
if(!in_array($page, $arr_allowed_pages)) $page = "error";
...
?>

<?php require_once ("$page.html");?>

Always validate input data, as it can be used to breach security.

Regards
Michal

Psychopsia

11:06 pm on Oct 30, 2006 (gmt 0)

10+ Year Member



Also I suggest you to check if the file exists, using file_exists [php.net]() function.

Light_Gear

1:10 am on Oct 31, 2006 (gmt 0)

10+ Year Member



Thanks,
I will try it out tomarrow at work. I think this will fix my problem,

Light_Gear

2:19 pm on Oct 31, 2006 (gmt 0)

10+ Year Member



Well it work but now I have created a problem that I could spend all day on or ask you guys and keep working on it.
Can I do sub folders or do I have to put all the file in the same directory.

Ah the more I do this the more I realize I do not know much of anything

Thanks

Light_Gear

2:32 pm on Oct 31, 2006 (gmt 0)

10+ Year Member



Figured it out was typing it in wrong

Thanks

Light_Gear

3:50 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



Hmmm new one ;-)

When I use my link ---> url index.php?page=about.php

can i make a database variable as well

ie I use to link /about.php?REPORT_NAME=name

and it would look up only those field that equal name

Now I have tried to do it using

index.php?page=about.php?REPORT_NAME=name

but it does not work.
If i use
index.php?page=about.php
I will get all the reports but it is the 2nd? that does me in

I gett error --->
An error occurred in script index.php on line 138: main(about/about.php?REPORT_NAME=about): failed to open stream: No such file or directory

any help would be greatful

mcibor

10:32 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



use simply two gets

index.php?page=about.php&REPORT_NAME=name

Then as you parse data from about.php file, then all the variables are passed through:

about.php:
<?php

echo ". The name of the town is ".$_GET['view'];
?>

index.php:
<?php

echo $_GET['view'];
require_once('about.php');
?>

index.php?view=Alamakota will output:
Alamakota. Then name of the town is Alamakota

Hope this cleares things for you.
Michal

Psychopsia

10:39 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



Also, you need to take all security precautions to deny requests like:

index.php?page=../../etc/shadow,v -- and system files.

Light_Gear

3:28 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



ok ;-) I am just full of questions right now and thanks everyone for your help.

I have some update on the website that are very long and want to limit to 200 char and have a dynamic link to that upate. I know it is possible can anyone point me in the right direction

Thanks