Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
easy content changer by link
Old 10-10-2007, 08:58 AM easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
I have an easy navigation system built with links like this:


PHP Code:
<!-- navigation -->
<a href="?page=bio">bio</a>
 
<!-- content -->
<?php 
$page 
= @$HTTP_GET_VARS["page"];
switch (
$page) {
case 
"bio": include("bio.php"); break;
case 
"photo": include("photo.php"); break;
case 
"contact": include("contact.php"); break;
// default switch //
default: include("empty.php");
}
?>

So the content can change by clicking the link.
Now I was asking myself... what if I have lots of links to add... like in a blog, with lots of links to add daily.
So than you always have to add a line in the case-structure too... double work?
Is there a solution for that problem?
An easy one?
__________________

Please login or register to view this content. Registration is FREE

Last edited by Bulevardi; 10-10-2007 at 10:13 AM..
Bulevardi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-10-2007, 09:56 AM Re: easy content changer by link
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Could you put that in tags? It makes the code more simple to read.

Yes there if, I believe the solution your looking for is:

PHP Code:
<?php
$page 
$_GET['page'];
if(
$page == "config"){ // block bad pages
} else { // the page does not do anything bad. 
if(@include("$page")){ // if it can be included.
} else {
include(
"empty.php");
}
}
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 10-10-2007 at 10:00 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 10-10-2007, 10:10 AM Re: easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
*adds talkupation ++*

Okay, so something like this would also work?



PHP Code:
<a href="?page=test">test</a>
 
 
<?php 
$page 
$_GET['page']; 
if (empty(
$page)) { 
include (
"empty.php"); 
} else { 
include(
"$page.php"); 

?>
Because the config part looks not familiar for me. Also the @include. (i'm still learning php basics)

Do I have to add .php in this example above ?
__________________

Please login or register to view this content. Registration is FREE

Last edited by Bulevardi; 10-10-2007 at 10:16 AM..
Bulevardi is offline
Reply With Quote
View Public Profile
 
Old 10-10-2007, 11:59 AM Re: easy content changer by link
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
That should work fine.

Though on a security note, it's very insecure to include exactly what the user says. For example if I felt evil and wanted to mess about with your page, I could change the URL query (?page=bit) to a root file, or something similar.

Oh, and putting '@' means it will check if the function runs, for example if I wanted to check if a file existed i would put

PHP Code:
if(@file_exists("config.php") == TRUE){ // if the file is found (with not errors if not found cause it's using the @)
// do some stuff to the file

Also, thanks for the Talkupation!
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 10-10-2007 at 12:03 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 10-10-2007, 03:27 PM Re: easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
And what do you have to type between
{ // do some stuff to the file }
to protect the system?<br><br>And what would it give when you surf to something like ?page=bit<br>It will give an error because that file doesn't exist?<br>And what do you have to put in config.php ? <br><br>I'm such a security n00b <br>
__________________

Please login or register to view this content. Registration is FREE
Bulevardi is offline
Reply With Quote
View Public Profile
 
Old 10-11-2007, 05:01 AM Re: easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
So my first example with cases is also unsecure?

I have to put this instead?

PHP Code:
 
<?php 
$page 
$_GET['page']; 
if (@
file_exists("config.php") == TRUE) { 
echo 
"Error!";
ifelse (empty($page)) { 
include (
"empty.php"); 
} else { 
include(
"$page.php"); 

?> 
 
 
or
 
<?php 
$page 
$_GET['page']; 
if (@
file_exists("config.php") == TRUE) { 
echo 
"Error!";
ifelse {
include(
"$page.php");
} else (empty(
$page)){
include (
"empty.php");
}
?>
__________________

Please login or register to view this content. Registration is FREE

Last edited by Bulevardi; 10-11-2007 at 05:37 AM..
Bulevardi is offline
Reply With Quote
View Public Profile
 
Old 10-11-2007, 06:25 AM Re: easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
It still gives an error when i make it "page=bit" in the adress bar.
__________________

Please login or register to view this content. Registration is FREE
Bulevardi is offline
Reply With Quote
View Public Profile
 
Old 10-11-2007, 11:43 AM Re: easy content changer by link
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Firstly:

Quote:
Originally Posted by Bulevardi View Post
PHP Code:
ifelse (empty($page)) { 
Ifelse? Surely you mean elseif:

PHP Code:
} elseif (empty($page)) { 
Secondly, your second example (after the 'or') doesn't make logical sense as your ifelse [sic] doesn't contain anything.

My advice use option one, and correct it like so:

PHP Code:
<?php 
$page 
$_GET['page']; 
if (@
file_exists("config.php") == TRUE) { 
echo 
"Error!";
} elseif (empty(
$page)) { 
include (
"empty.php"); 
} else { 
include(
"$page.php"); 

?>
Actually, I would also consider not having a default setting; otherwise one could in theory get the page to load a spam page or something else malicious; just use a series of elseif()s to allow only a set range of pages, if it is possible and convienient, of course.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-11-2007, 12:58 PM Re: easy content changer by link
Skilled Talker

Posts: 84
Location: Brussels, Belgium
Trades: 0
Quote:
Originally Posted by Foundationflash View Post
Ifelse? Surely you mean elseif:
Yeah, I know, it was a mistake.

I tried the code you posted, but I always got the error "Error!" written on my screen.

I tried it with several ways, but always Parse Errors, T_IS_EQUAL, T_VARIABLE, expected and unexpected { and ('s, ...


Quote:
Originally Posted by Foundationflash View Post
Actually, I would also consider not having a default setting; otherwise one could in theory get the page to load a spam page or something else malicious; just use a series of elseif()s to allow only a set range of pages, if it is possible and convienient, of course.
But if they change, they only get the change on their screen... they can 't change the pages on my server?
I can 't see how they can do something evil with it...


You mean I have to do it like this: with the elseif things:
PHP Code:
<?php 
                    
if (isset($_GET['page'])) 
                    { 
                        if(
$_GET['page']=='home'
                        { 
                            include(
'home.php'); 
                        } 
                        elseif(
$_GET['page']=='news'
                        { 
                            include(
'news.php'); 
                        } 
                        elseif(
$_GET['page']=='links'
                        { 
                            include(
'links.php'); 
                        } 
                    } 
                
?>
But than it's quite the same as the SWITCH trick in my first post.
Is it also unsecure with the SWITCH trick?
__________________

Please login or register to view this content. Registration is FREE

Last edited by Bulevardi; 10-11-2007 at 01:02 PM..
Bulevardi is offline
Reply With Quote
View Public Profile
 
Old 10-11-2007, 02:04 PM Re: easy content changer by link
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
I think this is a case of misunderstanding.

Quote:
Originally Posted by Bulevardi View Post
I tried the code you posted, but I always got the error "
Quote:
Originally Posted by Bulevardi View Post
Error!" written on my screen.


The bit about @file_exists() was just an example I think, *rereads it*, yes, just strip that bit out, or it will always do that (if it does exist).

Quote:
But if they change, they only get the change on their screen... they can 't change the pages on my server?
True. But they can display other pages (on their site etc) and kind of cover it up by viewing through a legitimate site such as your own.

Quote:
Is it also unsecure [sic] with the SWITCH trick?
I don't think it is insecure. By the way, who said it was insecure? I would do it with a SWITCH myself.

For the code, here you go (one way of doing it). I don't use a SWITCH, but you could just as easily:

PHP Code:
 <?php 
$page 
$_GET['page']; 
if (empty(
$page)) { 
include (
"empty.php"); 
} elseif(
$page == "page1" || $page == "page2" || $page == "page3"){ 
include(
"$page.php"); 

?>


__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Reply     « Reply to easy content changer by link
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30586 seconds with 12 queries