Current location: Hot Scripts Forums » Programming Languages » PHP » Parsing a query string


Parsing a query string

Reply
  #1 (permalink)  
Old 09-18-04, 04:02 AM
lisa_maria lisa_maria is offline
New Member
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Parsing a query string

Hi! Guys

I'm newbie on PHP, and I desperately need your help on this. I'm trying to make something like: http://www.mysite.com/index.php?page=home
I have written the following code, but it doesn't work. Please...?

<?php
switch( $_GET['page'] ) {
case "home" : $page="home.php"; break;
case "collections" : $page="collections.php"; break;
case "catalogs" : $page="catalogs.php"; break;
case "contact" : $page="contact.php"; break;
default: $page="index.php"; break;
}
?> <a href="<?php $_GET['?page=home']; ?>" target="_self" class="style1">ENTER</a>

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 09-18-04, 06:34 PM
tylerc tylerc is offline
Newbie Coder
 
Join Date: May 2004
Location: Canada
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
ok, do it like this the name of $page has to be the filename unfortuneatly, but more efficient!

PHP Code:

<?php


parse_str
($_SERVER["QUERY_STRING"]);

if (
file_exists($page ".php")) {
include(
$page ".php");
}
else {
include(
"index.php");
}
?>
This Help?
__________________
-Tyler
RuneGuide Owner/Manager

Runescape Stats:
http://www.rsbandb.com/sig/sigs/n/nexon89.png
Reply With Quote
  #3 (permalink)  
Old 09-18-04, 06:46 PM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
tylerc, your solution is very dangerous, an open door for hackers. The first solution was better. I'm not sure, but I think your script would work if you would write <?php echo $_GET['?page=home']; ?> instead of <?php $_GET['?page=home']; ?>
__________________
http://www.hardcoded.net
Reply With Quote
  #4 (permalink)  
Old 09-19-04, 03:01 AM
lisa_maria lisa_maria is offline
New Member
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Guys, thanks for your help! I have modified the code a bit to make more sense. Now, my problem is that when I click on "ENTER" I can't get the i.e. 'home' page. the url shows the "index.php?page=..." and just reloads the index.php.

I was hoping when the user clicks on ENTER to go to 'home' page.

<?php
switch( $_GET['page'] ) {
case 'home' : $page= 'home.php' ; break;
case "collections" : $page='collections.php' ; break;
case "catalogs" : $page='catalogs.php' ; break;
case "contact" : $page='contact.php' ; break;
default: $page='index.php' ; break;
}
print '<a href="index.php?page=' . $_GET['$page'] . '">ENTER</a>'; ?>
Reply With Quote
  #5 (permalink)  
Old 09-19-04, 10:34 PM
rjwebgraphix rjwebgraphix is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by lisa_maria
Hi! Guys

I'm newbie on PHP, and I desperately need your help on this. I'm trying to make something like: http://www.mysite.com/index.php?page=home
I have written the following code, but it doesn't work. Please...?
I am also new to PHP. I will show you what I did, which there are probably better ways of doing it, but this works for me....

The following code is put in the head to determine if nothing has been assigned to the variable $content and then assigns the home page to that variable.

PHP Code:

  <?PHP if ($content == ""){ $content home; } ?>

Next is the code that displays the $content page(s) It checks for extentions .html, .php, .txt, and no extention and displays those pages, if it does not find it, it will refresh to the home page. There are more things you can do with the end of this. At some point I plan on changing this to reload an error page if the page doesn't exist as I would prefer that.

PHP Code:

<!BeginPHPContent>

<?php
if(file_exists("$content.html"))
{
   include (
"$content.html");
}
elseif (
file_exists("$content.php"))
{
   include (
"$content.php");
}
elseif (
file_exists("$content.txt"))
{
   include (
"$content.txt");
}
elseif (
file_exists("$content"))
{
   include (
"$content");
}
else
{
   echo 
"<meta http-equiv=refresh content=0;url=./index.php?content=home>";
}
?>           
<!EndPHPContent>
The next is the code for my links. This will look confusing as all get out as I had to put more code than I wanted to originally because I have an image between each link and and the lesser code method put a space between the image link and the image between the link where they need to be touching eachother.

This portion checks to see what page your on and displays the appropriate link image for if you are on the page or not. There is also a javascript that goes with this for mouseover effects. So in effect it will display one image if you are on THAT page. Another image if you are not on that page and a 3rd image altogether if you move the mouse over the link.

PHP Code:

<! -- Begin Link Table -- >

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
    <?PHP if($content == "home"): ?>
          <TR>
        <TD><IMG SRC="rj_link_home_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('home','rj_link_home_mouseon.jpg')" onmouseout="move_out('home','rj_link_home_nomouse.jpg')" HREF="./index.php?content=home">
            <IMG NAME="home" ALT="Home Page" SRC="rj_link_home_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "packages"): ?>
          <TR>
        <TD><IMG SRC="rj_link_packages_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('packages','rj_link_packages_mouseon.jpg')" onmouseout="move_out('packages','rj_link_packages_nomouse.jpg')" HREF="./index.php?content=packages">
            <IMG NAME="packages" ALT="packages Page" SRC="rj_link_packages_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "samples"): ?>
          <TR>
        <TD><IMG SRC="rj_link_samples_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('samples','rj_link_samples_mouseon.jpg')" onmouseout="move_out('samples','rj_link_samples_nomouse.jpg')" HREF="./index.php?content=samples">
            <IMG NAME="samples" ALT="samples Page" SRC="rj_link_samples_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "faq"): ?>
          <TR>
        <TD><IMG SRC="rj_link_faq_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('faq','rj_link_faq_mouseon.jpg')" onmouseout="move_out('faq','rj_link_faq_nomouse.jpg')" HREF="./index.php?content=faq">
            <IMG NAME="faq" ALT="faq Page" SRC="rj_link_faq_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "hosting"): ?>
          <TR>
        <TD><IMG SRC="rj_link_hosting_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('hosting','rj_link_hosting_mouseon.jpg')" onmouseout="move_out('hosting','rj_link_hosting_nomouse.jpg')" HREF="./index.php?content=hosting">
            <IMG NAME="hosting" ALT="hosting Page" SRC="rj_link_hosting_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "about"): ?>
          <TR>
        <TD><IMG SRC="rj_link_about_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('about','rj_link_about_mouseon.jpg')" onmouseout="move_out('about','rj_link_about_nomouse.jpg')" HREF="./index.php?content=about">
            <IMG NAME="about" ALT="about Page" SRC="rj_link_about_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

    <?PHP if($content == "contact"): ?>
          <TR>
        <TD><IMG SRC="rj_link_contact_onpage.jpg" BORDER="0" WIDTH="93" HEIGHT="31"></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP else: ?>
          <TR>
        <TD><A onmouseover="move_in('contact','rj_link_contact_mouseon.jpg')" onmouseout="move_out('contact','rj_link_contact_nomouse.jpg')" HREF="./index.php?content=contact">
            <IMG NAME="contact" ALT="contact Page" SRC="rj_link_contact_nomouse.jpg" WIDTH="93" HEIGHT="31" BORDER="0"></A></TD>
          </TR>
          <TR>
        <TD VALIGN="Top"><IMG SRC="rj_linkbetween.jpg" WIDTH="93" HEIGHT="7" BORDER="0"></TD>
        
          </TR>
    <?PHP endif; ?>

</TABLE>
<! End Link Table>
Hope this helps you.

RJ
Reply With Quote
  #6 (permalink)  
Old 09-20-04, 03:48 PM
tylerc tylerc is offline
Newbie Coder
 
Join Date: May 2004
Location: Canada
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Its not a security risk if you know what you are doing, see in the index.php file you do this:

PHP Code:

define('in_script','1'); 

Then in the page that you are loading type this:

PHP Code:

if (!defined('in_script')) {

die(
"Hacking is bad for your health!");

Then it aint a risk
__________________
-Tyler
RuneGuide Owner/Manager

Runescape Stats:
http://www.rsbandb.com/sig/sigs/n/nexon89.png
Reply With Quote
  #7 (permalink)  
Old 09-20-04, 04:32 PM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
In fact, I don't know much about security, but I read an insightful comment on these boards.

What if you would call http://www.yoursite.com/index.php?page=/etc/passwd%00 ? I didn't try it, but I think that the hacker will have access to your passwords' md5 (and can brute force them after.)

and what if I call it http://www.yoursite.com/index.php?pa...tructivescript ?

I am not sure, but I think that in this case, the server would execute that script.

As I said, I don't know much about security, and maybe these 2 examples don't work. However, I think that giving that much power (Letting a visitor choose the argument he wants for the "include" function) is dangerous.

As for the "define" example you gave, I don't see how it would help security. If I am not mistaken, the only thing is achieves is to disallow the call of content scripts outside index.php. But calling these content script outside index.php is not a security threat. The threat is calling an unexpected script/file from index.php.
__________________
http://www.hardcoded.net
Reply With Quote
  #8 (permalink)  
Old 09-20-04, 07:36 PM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
Hardcoded your mostly right but, http://www.yoursite.com/index.php?page=/etc/passwd%00 would, assuming it's an apache, look for /www/etc/passwd or for any other server root_dir then etc/passswd s try: http://www.yoursite.com/index.php?pa.../etc/passwd%00 Then it's would include it since the passwd's are not accessable by http usally
Reply With Quote
  #9 (permalink)  
Old 09-20-04, 09:43 PM
rjwebgraphix rjwebgraphix is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by hardcoded
In fact, I don't know much about security, but I read an insightful comment on these boards.

What if you would call http://www.yoursite.com/index.php?page=/etc/passwd%00 ? I didn't try it, but I think that the hacker will have access to your passwords' md5 (and can brute force them after.)

and what if I call it http://www.yoursite.com/index.php?pa...tructivescript ?

I am not sure, but I think that in this case, the server would execute that script.
I just did some checking on this and how I've been using the includes. Your absolutely correct!!!!! I tried the index.php?content=/etc/passwd as well as many other files located in my /etc directory. HOLY $#!+ that is a HUGE security risk. Sure enough it listed the passwd file. Granted, it doesn't show passwords in a passwd file and the shadow file does not exist in the /etc directory, but it's still a risk.

I'm going to try what tylerc put and see how/if it works, but the only way I can truely see is to test the variable and see if it is one that I want to load, if not then display an error page.

I wonder if I can test this prior to loading the full page. IE: Before the <head>

Trying to figure out how to test that one variable equals one of 7 possibilities then continue loading the page. Thing that worries me is what if this turns into one of 100 possibilities in the future. Or worse yet, if there is a need for it to be one of 10,000 possibilities. I don't have a site THAT huge, but it is possible. Oh Geez! I'm brain dead at the moment, will have to think about this one.
Reply With Quote
  #10 (permalink)  
Old 09-21-04, 06:10 AM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
What I did with my old script (As I said in another comment somewhere, this method is, IMHO, ugly, and slow, I now use XSLT) is that I listed all my pages in a mySQL database. The script looked for the pageneame, and if it was in the database, it returned the path that was specified on that row. Ok, this was slower, but the code was nicer.
__________________
http://www.hardcoded.net
Reply With Quote
Reply

Bookmarks


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
retrieving specific data from parsed query string grayfox Perl 1 04-04-04 11:38 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Mysql query string help nefariousphp PHP 3 12-08-03 06:51 AM
ASP SQL query string problem CollinAmes ASP 2 10-24-03 11:09 AM


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