Current location: Hot Scripts Forums » Programming Languages » PHP » Checking the $_GET


Checking the $_GET

Reply
  #1 (permalink)  
Old 05-31-09, 04:42 PM
captainsquid captainsquid is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Checking the $_GET

Hello, I am working on a site that will have content in different categories. Within the categories, there will be subjects. Within the subjects, there will be articles. I came up with this that will determine whether the article number, subject, or category was set. An example URL would be:
Code:
index.php?category=cats&subject=tails&article=tails suck
here is my logic (might be very bad):
Check if article was set ($_GET).
if it was, combine whatever was set for the category, subject, and article into a filename.php.
If it wasnt,
check wether a subject was set....etc..
here is what i have, but only the code above the script will show up in the browser.(only the header)
PHP Code:

<?php

      
$home 
"home.php";
$footer "footer.php";
$category $_GET['category'];
$subject $_GET['subject'];
$article $_GET['article'];
$phpend ".php";

function 
showfooter(){
    include (
"$footer");
}
function 
checksub(){
    if(empty(
$subject)) {
    
checkcat();
} elseif (isset(
$subject)){
    include (
"$category$subject$phpend");
    
showfooter();
    exit();
    }
}

function 
checkcat(){
    if(empty(
$category)) {
    include (
"$home");
    
showfooter();
} elseif (isset(
$category)){
    include (
"$category$phpend");
    
showfooter();
    exit();
    }
}

if(empty(
$article)) {
    
checksub();
} elseif (isset(
$article)){
    include (
"$category$subject$article$phpend");
    
showfooter();
    exit();
    }
    
      
?>
How would i fix this? I was guessing syntax was the problem, but i check and rechecked...maybe my logic is just failing me today...did I use the exit(); command inappropriately? thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 05-31-09, 06:45 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Take a look at this page: PHP: Variable scope - Manual
Reply With Quote
  #3 (permalink)  
Old 05-31-09, 08:50 PM
captainsquid captainsquid is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, nico!
the problem was that the variables declared only worked on a general scale, and did not apply within the functions. i had to globally declare each variable in each funtion. other functions did not have to be declared within the funtions:

PHP Code:

$home "home.php";

$footer "footer.php";
$category $_GET['category'];
$subject $_GET['subject'];
$article $_GET['article'];
$phpend ".php";

function 
showfooter(){
    global 
$footer;
    include (
"$footer");
}

function 
checksub(){
    global 
$subject;
    global 
$category;
    global 
$phpend;
    if(empty(
$subject)) {
    
checkcat();
} elseif (isset(
$subject)){
    include (
"$category$subject$phpend");
    
showfooter();
    exit();
    }
}

function 
checkcat(){
    global 
$category;
    global 
$phpend;
    global 
$home;
    if(empty(
$category)) {
    include (
"$home");
    
showfooter();
} elseif (isset(
$category)){
    include (
"$category$phpend");
    
showfooter();
    exit();
    }
}

if(empty(
$article)) {
    
checksub();
} elseif (isset(
$article)){
    include (
"$category$subject$article$phpend");
    
showfooter();
    exit();
    }
    
      
?> 
Reply With Quote
  #4 (permalink)  
Old 06-01-09, 02:52 AM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
You have a major security hole in your script. You must never use $_GET vars or any user submitted information without filtering it first. On top of that you then include that variable. Basically you wrote the equivalent of "do whatever you want my site" security hole.

To paint a clear picture, set $_GET['category'] = 'file of interest' and the others to null and you script without provocation will execute
include(whateveryouwant.php)

even with your phpEnd it doesn't help because someone can just as easily negate that if they know it's there.

understand that is an issue?
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads

Last edited by infinitylimit; 06-01-09 at 02:55 AM.
Reply With Quote
  #5 (permalink)  
Old 06-03-09, 11:07 PM
captainsquid captainsquid is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
so, are you saying that i should set an array? I dont really understand what you mean by 'filter'. I do notice the security hole, and see how it could possibly lead to some leaked info... could you please show an example in which something terrible could happen? thanks...
Reply With Quote
Reply

Bookmarks

Tags
php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP sockets - checking connection.. HELP!!! Turboz PHP 1 12-30-05 03:53 PM
just checking ashoka323 The Lounge 0 02-13-05 06:26 PM
Checking MYSQL for duplicate entry.. doublee313 PHP 2 09-24-04 09:05 AM
New Alternative to WhatIsMyIP for IP checking - FlashMyIP.com akaz The Lounge 0 09-19-04 01:27 PM
checking table types in Mysql rocky PHP 1 08-01-04 05:06 AM


All times are GMT -5. The time now is 08:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.