Forum Moderators: coopster

Message Too Old, No Replies

navigation via "index.php?page=home" style links

coding with register_globals off? help!

         

dnb_ovst

9:07 pm on Jan 8, 2006 (gmt 0)

10+ Year Member



Hey guys. Ive been hounding google for the past two days looking for a tutorial on how to use php includes for site navigation.
-Ive found one that does the job, but it can only work with register_globals ON:

<div id="content">
<?php
switch($page)
{
default: include('home.inc');
break; case "home": include('home.inc');
break; case "podcast": include('podcast.inc');
break; case "photos": include('photos.inc');
break; case "id": include('id.inc');
break; case "friends": include('friends.inc');
}
?>
</div>
<div id="links>
<a href="home.php?page=home">home</a>
<a href="home.php?page=podcast">podcast</a>
<a href="home.php?page=photos">photos</a>
<a href="etc..."
</div>

However, ive been reading the register_globals ON setting is a compromise to security. so i turned it off. needless to say the above script doesnt work anymore...

-Then i found this script:

<div id="content">
<?php
$load = array ('home','podcast','photos','id','friends');
$page = $_GET['$page'];
if (isset (in_array ($page, $load) ))
{
require "$page.inc";
}
else
{
require "home.inc";
}
?>
</div>
<div id="links>
<a href="home.php?page=home">home</a>
<a href="home.php?page=podcast">podcast</a>
<a href="home.php?page=photos">photos</a>
<a href="etc..."
</div>

-This one LOOKED like it should work. it declares the variables beforehand, to adhere to the register_globals setting being off. but it doesnt work at all? i have no clue whats wrong with it. when i access the page via http, the html source code shows up blank?

ANY help or suggestions would help alot! thanks!

dreamcatcher

9:38 pm on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing this:

$page = $_GET['$page'];

to this:

$page = $_GET['page'];

dc

dnb_ovst

10:18 pm on Jan 8, 2006 (gmt 0)

10+ Year Member



hmm.. no good.. still getting a blank page. thanks for the idea though-

also, going through the script, is the logic valid? maybe cuz im using php5 it wont work? i dunno...

also #2, is there a way to turn register_globals on, just for this site and this site only (leaving any other sites on the server unaffected: register_globals still off)? all im using php for is: navigation, email, and a shoutbox... would it still be a threat to security?

henry0

11:44 pm on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try to echo all of your var to be sure that they are accessible?

Did you try adding
error_reporting (E_ALL);
and read the result?

dnb_ovst

12:11 am on Jan 9, 2006 (gmt 0)

10+ Year Member



okay i got it working.. finally. no more help needed! but just to follow up:

the second script was just horrible from the get-go.. i dont know exactly what it was (im new to php) but i figured it had something to do with the "if (isset ( etc. " part. although they made perfect sense to me.

i tried fixing it too many times, then gave up. so i went back to the first script which worked (the one that was valid with register_globals ON). im using php5 and which im sure most of you's know... register_globals is OFF by default. So, not declaring your variables first is a no-no.

What i did was sooo elementary.. i feel dumb for not thinking to do it in the first place. heres how i changed the script:

_ORIGINAL______________________


<div id="content">
<?php
switch($page)
{
default: include('home.inc');
break; case "home": include('home.inc');
break; case "podcast": include('podcast.inc');
break; case "photos": include('photos.inc');
break; case "id": include('id.inc');
break; case "friends": include('friends.inc');
}
?>
</div>
<div id="links">
<a href="home.php?page=home">home</a>
<a href="home.php?page=podcast">podcast</a>
<a href="home.php?page=photos">photos</a>
<a href="etc..."
</div>

_REVISED_______________________


<div id="content">
<?php

// all i had to do was declare my globals...
$load = array ('home','podcast','photos','id','friends');
$page = $_GET['page'];
// Im retarted, i know

switch($page)
{
default: include('home.inc'); break;
case "home": include('home.inc'); break;
case "podcast": include('podcast.inc');break;
case "photos": include('photos.inc'); break;
case "id": include('id.inc'); break;
case "friends": include('friends.inc'); break;
}
?>
</div>
<div id="links">
<a href="home.php?page=home">home</a>
<a href="home.php?page=podcast">podcast</a>
<a href="home.php?page=photos">photos</a>
<a href="etc..."
</div>

so yeah. i wasted too much time looking for extra resources to copy from.. until i finally decided to take a crack at it myself. low and behold... amazing. i feel amazing.

just a thought.. the second script i tried i actually liked alot better. it was much more leaner. can anyone pinpoint what was wrong with the logic? im almost certain its the if (isset (in_array ($page, $load) )) condition/statement/whatever..?

henry0

1:04 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are all of those available?
did you change any of those names in the array?

('home','podcast','photos','id','friends')

dnb_ovst

10:02 pm on Jan 10, 2006 (gmt 0)

10+ Year Member



henry- not quite sure what youre reffering to... 'those' meaning, the actual files in the directory? or the variables?

but- the files are there and, nope.. havent changed the variables...?

FalseDawn

3:51 am on Jan 11, 2006 (gmt 0)

10+ Year Member



I don't think you need the isset - the in_array returns true/false - just use

if (in_array($page, $load))
{ ...

}
etc

also,
<div id="links> has a missing closing " - typo?

If you are getting a blank page there is probably an error in your script - turning on error reporting will help.

dnb_ovst

4:35 am on Jan 14, 2006 (gmt 0)

10+ Year Member



falsedawn- thanks for the suggestion. i took out the 'isset', looks like i could be making some progress starting with that- but i still havent been able to get the site fully working using that script... when i took out the 'isset' it at least gave me some html content back (before all i could see was a blank page). What i see now is only the html before my php script, the rest is blank. (the content as well as closing tags for the <div>'s/ <body>/ <html> are missing)

as far as error reporting, i attempted using henry0's suggestion and placed '<?php error_reporting (E_ALL);?>' in my document. however, it didnt output any errors? then i thought im probably putting it in the wrong spot, so i tried using it in several locations: before the <html> tag, after the <html> tag, in the <head> section, and in the <body> section.... nothin-

now im thinking at this point, could this be a configuration issue?

heres a look at the script again:
<?php

$load = array ('home','podcast','photos','id','friends');
$page = $_GET['page'];
if (in_array('$page', '$load'))
{
require "$page.inc";
}
else
{
require "home.inc";
}

?>

thanks guys

stu_uk

11:19 am on Jan 14, 2006 (gmt 0)

10+ Year Member



the only problem with this method is it produces ugly URLs which may hinder your SERPS. Definatly mod-Rewrite to more SE friendly onces.

stu_uk

11:24 am on Jan 14, 2006 (gmt 0)

10+ Year Member



the problem in the last bit of code is you have the variables as literals in the in_array() function,

it should be

if (in_array ($page, $load))

not

if (in_array ('$page', '$load'))

dnb_ovst

4:34 am on Jan 15, 2006 (gmt 0)

10+ Year Member



stu, yeah... i tried that too. it doesnt work... i dunno. i feel like maybe the whole script is written wrong? is there another method to use besides if..else?

henry0

3:33 pm on Jan 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll like to look at the whole thing
would you email me a link to the script
thanks