[SOLVED] $_GET['ting'] Pages (Safely) With PHP

This is a discussion on "[SOLVED] $_GET['ting'] Pages (Safely) With PHP" within the PHP Forum section. This forum, and the thread "[SOLVED] $_GET['ting'] Pages (Safely) With PHP are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Nov 4th, 2007, 14:12
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] $_GET['ting'] Pages (Safely) With PHP

Having some problems with this article

Quote:
[Sun Nov 04 15:10:56 2007] [error] PHP Parse error: syntax error, unexpected ')' in d:\\Apache\\htdocs\\test\\index.php on line 6
and this is the line in question
PHP: Select all

$page=(preg_match('/(\.\.|\/)/i',)?'home':$page); 

Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Nov 4th, 2007, 14:26
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Maybe the following?
PHP: Select all


$page
=(preg_match('/(\.\.|\/)/i'),?'home':$page); 
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Nov 4th, 2007, 14:52
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Nope...didn't work

Quote:
[Sun Nov 04 15:50:35 2007] [error] PHP Parse error: syntax error, unexpected ',' in d:\\Apache\\htdocs\\test\\index.php on line 6
what did work however is if i took the comma out it displayed the menu but didn't include the home page (i'm just working with the examples in the article)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Nov 4th, 2007, 15:15
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

yep, there's an error there. sorry bout that

It should be:
PHP: Select all

$page=(preg_match('/(\.\.|\/)/i',$page)?'home':$page); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Nov 4th, 2007, 16:05
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

I'll change this in a minute guys.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Nov 4th, 2007, 16:29
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Fixed! Shame on our tester
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Nov 5th, 2007, 12:34
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Alex....you need to change that bit in the finished code also becuase that still reads the same
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Nov 5th, 2007, 15:21
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Okay will now.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Nov 5th, 2007, 17:03
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Fixed (I think).
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Nov 5th, 2007, 19:59
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Still getting problems

Quote:
[Mon Nov 05 20:59:41 2007] [error] PHP Warning: main(./pageserror.php) [<a href='function.main'>function.main</a>]: failed to open stream: No such file or directory in d:\\Apache\\htdocs\\test\\index.php on line 15
[Mon Nov 05 20:59:41 2007] [error] PHP Warning: main() [<a href='function.include'>function.include</a>]: Failed opening './pageserror.php' for inclusion (include_path='.;d:\\php\\includes;D:\\php\\PEAR') in d:\\Apache\\htdocs\\test\\index.php on line 15
[Mon Nov 05 20:59:41 2007] [error] PHP Notice: Undefined variable: title in d:\\Apache\\htdocs\\test\\index.php on line 19
[Mon Nov 05 20:59:41 2007] [error] PHP Notice: Undefined variable: content in d:\\Apache\\htdocs\\test\\index.php on line 33
I noticed some of the code doesn't match futher up the page than further down the page in the finished bit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Nov 6th, 2007, 05:28
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

I think you are missing a slash:
Code: Select all
./pageserror.php
should probably be this:
Code: Select all
./pages/error.php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Nov 6th, 2007, 08:54
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

This is what I have

PHP: Select all

<?php
// check the $_GET['page'] variable
$page = ((isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'home');
//  prevent file browsing
$page=(preg_match('/(\.\.|\/)/i',$page)?'home':$page);
// replace illegal characters
$page preg_replace('/[^a-zA-Z0-9 \._-]/','',$page);
// check if the requested file exists
$page = (file_exists('./pages'.$page.'.php') ? $page 'error');
// and include the page
include('./pages'.$page.'.php');
?>
<html>
<head>
<title><?php echo($title); ?></title>
<body>
 <div class="menu">
  <ul>
   <li><a href="index.php?page=home">HOME</a></li>
   <li><a href="index.php?page=contact">CONTACT</a></li>
   <li><a href="index.php?page=links">LINKS</a></li>
   <li><a href="index.php?page=products">PRODUCTS</a></li>
  </ul>
 </div>
 <div class="content">
  <?php echo($content); ?>
 </div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Nov 6th, 2007, 09:08
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Yep, you're missing a slash. Corrected:
PHP: Select all

<?php
// check the $_GET['page'] variable
$page = ((isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'home');
//  prevent file browsing
$page=(preg_match('/(\.\.|\/)/i',$page)?'home':$page);
// replace illegal characters
$page preg_replace('/[^a-zA-Z0-9 \._-]/','',$page);
// check if the requested file exists
$page = (file_exists('./pages'.$page.'.php') ? $page 'error');
// and include the page
include('./pages/'.$page.'.php');
?>
<html>
<head>
<title><?php echo($title); ?></title>
<body>
 <div class="menu">
  <ul>
   <li><a href="index.php?page=home">HOME</a></li>
   <li><a href="index.php?page=contact">CONTACT</a></li>
   <li><a href="index.php?page=links">LINKS</a></li>
   <li><a href="index.php?page=products">PRODUCTS</a></li>
  </ul>
 </div>
 <div class="content">
  <?php echo($content); ?>
 </div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Nov 6th, 2007, 09:59
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

Nearly working!!!!

I created an error page and now i get the menu listed but also the 404 error page instead of the home page.

I did the same example as in the article just for testing purposes.

I have a folder called pages with all the home, contact and 404 error page in so i don't know what the problem is
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Nov 6th, 2007, 11:45
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: $_GET['ting'] Pages (Safely) With PHP

hehe, Missed that error too. You also have to add a slash in the file_exists() function otherwise PHP won't find the page. Corrected again:
PHP: Select all

<?php
// check the $_GET['page'] variable
$page = ((isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'home');
//  prevent file browsing
$page=(preg_match('/(\.\.|\/)/i',$page)?'home':$page);
// replace illegal characters
$page preg_replace('/[^a-zA-Z0-9 \._-]/','',$page);
// check if the requested file exists
$page = (file_exists('./pages/'.$page.'.php') ? $page 'error');
// and include the page
include('./pages/'.$page.'.php');
?>
<html>
<head>
<title><?php echo($title); ?></title>
<body>
 <div class="menu">
  <ul>
   <li><a href="index.php?page=home">HOME</a></li>
   <li><a href="index.php?page=contact">CONTACT</a></li>
   <li><a href="index.php?page=links">LINKS</a></li>
   <li><a href="index.php?page=products">PRODUCTS</a></li>
  </ul>
 </div>
 <div class="content">
  <?php echo($content); ?>
 </div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Nov 6th, 2007, 13:18
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, Engl