GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Who can write this PHP properly? (https://gfy.com/showthread.php?t=842612)

potter 07-20-2008 01:26 PM

Who can write this PHP properly?
 
Ok, so I can't write php for the life of me. I have this script written out, kind of, but need it properly written as mine is pretty fubar.

Code:

<?
$ID = $_GET['ID'];
if($ID == ('1')) {
        include ('header.php');
        include ('1.php');
        include ('footer.php');
}
elseif ($ID == ('2')) {
        include ('header.php');
        include ('2.php');
        include ('footer.php');
}
elseif ($ID == ('3')) {
        include ('header.php');
        include ('3.php');
        include ('footer.php');
}
else {
        include ('noid.php');
}
?>

So basically if someone goes to said page.php?ID= and the ID is there it'll spit out the header and footer with a content page in the middle relating to that ID. If there is no ID then it'll include a completely different page all together.

I know this is easy stuff. I probably have everything from the way it grabs the ID, to the way it decides what to output. But alas I don't know shit about writing php. :upsidedow

chemicaleyes 07-20-2008 01:42 PM

Bump for ya :glugglug

Sands 07-20-2008 01:43 PM

Errr, use a switch statement.

Bro Media - BANNED FOR LIFE 07-20-2008 01:45 PM

PHP Code:

<?php
include("header.php");
if(
$_GET['id'] == true)
{
  if(
file_exists($_GET['id'] . ".php")
  {
    include(
$_GET['id'] . ".php");
  }else{
    include(
"noid.php");
  }
}else{
  include(
"noid.php");
}
include(
"footer.php");
?>

quick and dirty but it should give you alittle help on what you want to do

Sands 07-20-2008 01:46 PM

PHP Code:

<?php
    $id 
$_GET['id'];
    switch (
$id) {
        case 
1:
            include (
'header.php');
            include (
'1.php');
            include (
'footer.php');            
            break;
        case 
2:
            include (
'header.php');
            include (
'1.php');
            include (
'footer.php');            
            break;
        case 
3:
            include (
'header.php');
            include (
'1.php');
            include (
'footer.php');            
            break;
        default:
            include (
'noid.php');
    }
?>

I think this will work.

Voodoo 07-20-2008 01:47 PM

PHP Code:

<?php
$ID
=$_GET['ID'];
include (
'header.php');
switch (
$ID) {
case 
1:
    include (
'1.php');
    break;
case 
2:
    include (
'2.php');
    break;
case 
3:
    include (
'3.php');
    break;
default:
    include (
'noid.php');
}
include (
'footer.php');
?>


jimbona 07-20-2008 01:47 PM

Code:

$allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
$ID = intval($_GET['ID']);
$page = "";
if(array_key_exists($ID,$allowed_ids))
{
        $page = $allowed_ids[$ID];
        include ('header.php');
        include ($page);
        include ('footer.php');
}
else
{
        include ('noid.php');
}


Voodoo 07-20-2008 01:48 PM

Or here's another method...
PHP Code:

<?php
$ID
=$_GET['ID'];
include (
'header.php');
include (
'$ID.php');
include (
'footer.php');
?>


Voodoo 07-20-2008 01:50 PM

Depends on how secure you want to make this. If it's just a simple include page passed via the URL, then the last one has the fewest lines, otherwise, you can do the various checks for a valid filename etc...

potter 07-20-2008 01:54 PM

Quote:

Originally Posted by jimbona (Post 14482846)
Code:

$allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
$ID = intval($_GET['ID']);
$page = "";
if(array_key_exists($ID,$allowed_ids))
{
        $page = $allowed_ids[$ID];
        include ('header.php');
        include ($page);
        include ('footer.php');
}
else
{
        include ('noid.php');
}


Nice, I like that allowed arrays part.

Thanks a bunch everyone. Going to take these back and try em out on my project. Be back in a little bit. :):thumbsup

Bro Media - BANNED FOR LIFE 07-20-2008 02:01 PM

Quote:

Originally Posted by jimbona (Post 14482846)
Code:

$allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
$ID = intval($_GET['ID']);
$page = "";
if(array_key_exists($ID,$allowed_ids))
{
        $page = $allowed_ids[$ID];
        include ('header.php');
        include ($page);
        include ('footer.php');
}
else
{
        include ('noid.php');
}


:thumbsup:thumbsup:thumbsup

potter 07-20-2008 02:15 PM

Ok, I definitely like the idea of checking that the id input is valid. However with jimbona's code snippet it only seems to load noid.php no matter what.

Brujah 07-20-2008 02:31 PM

Another option:

Code:

<?php
$id = $_GET['id'];
$page = preg_replace('/[^a-z0-9]+/', '', $id);
if( is_file("$page.php") ) {
  include('header.php');
  include("$page.php");
  include('footer.php');
} else {
  include('noid.php');
}
?>


Hotrocket 07-20-2008 02:38 PM

Love watching coders one upping each other with their versions..and then the next...and the next...lol

GrouchyAdmin 07-20-2008 02:44 PM

I would make a really funny enormous bells-and-whistles one that lets you ban IP addresses by subnet and does some really neat stuff like signaling through apache_note(), but I lack the time, mrkris isn't here, and I'm not coding today.

Instead, have the world's worst implementation.

Code:

<?=include("header.php";(file_exists($_REQUEST['id'])?include($_REQUEST['id']):include("noid.php"));include("footer.php");?>
I will say, though, that Sands' use of switch is, well, a better abuse of logic than a ton of if/else/elif statements.

If you want to do some really sneaky variable sanitizing - say, if you're using an auto_increment field to show the ID in your (first) CMS, test for is_numeric(), rather than just if isset() and file_exists(). Also, might be a good time to pick up on regex so you can deny things like ?id=../../../etc/passwd

GeXus 07-20-2008 02:46 PM

I don't code for free... but if you want something done right.. hit me up

GrouchyAdmin 07-20-2008 02:47 PM

Quote:

Originally Posted by GeXus (Post 14483099)
I don't code for free... but if you want something done right.. hit me up

In my experience: If you charge for a simple 'please help' PHP 101, you're not likely to get a lot of response from the peanut gallery.

fris 07-20-2008 03:19 PM

Quote:

Originally Posted by Voodoo (Post 14482855)
Or here's another method...
PHP Code:

<?php
$ID
=$_GET['ID'];
include (
'header.php');
include (
'$ID.php');
include (
'footer.php');
?>


need to put error trapping in if no id is sent.

Bro Media - BANNED FOR LIFE 07-20-2008 03:39 PM

i was dumb founded when i attempted this, you know how long its been since i included a file via url parameters? heh... when somethings sent from the url, its for database reasons...

Sands 07-20-2008 03:40 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14483092)
I will say, though, that Sands' use of switch is, well, a better abuse of logic than a ton of if/else/elif statements.

I abuse my logic like a red-headed orphan.

xentech 07-20-2008 03:46 PM

Code:

<?php
switch ($id)
{
case 1:
include("header.php");
  break;
case 2:
include("header.php");
  break;
case 3:
include("header.php");
  break;

default:
  echo "No number between 1 and 3";
}

?>


xentech 07-20-2008 03:48 PM

Wow, I posted that not seeing the other options people have posted. A simple switch command is genius and in my opinion is easily the best way of coding a site's navigation.

k0nr4d 07-20-2008 04:04 PM

Quote:

Originally Posted by Voodoo (Post 14482855)
Or here's another method...
PHP Code:

<?php
$ID
=$_GET['ID'];
include (
'header.php');
include (
'$ID.php');
include (
'footer.php');
?>


Fail.
First off, you are using single quotes which means it would have to be include($ID.'.php'); not the way you have it, secondly what if i call it with file.php?id=/some/other/sites/on/this/server/phpfile ?

GeXus 07-20-2008 04:07 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14483106)
In my experience: If you charge for a simple 'please help' PHP 101, you're not likely to get a lot of response from the peanut gallery.

In my experience, If you do free work, you're wasting your time.

Voodoo 07-20-2008 04:12 PM

Quote:

Originally Posted by k0nr4d (Post 14483355)
Fail.
First off, you are using single quotes which means it would have to be include($ID.'.php'); not the way you have it, secondly what if i call it with file.php?id=/some/other/sites/on/this/server/phpfile ?

You should read my post right under this one. I did a couple versions, and indicated that he could use that method if he wasn't concerned with security. There are many ways to do what he's asking for. I'm just giving him options.

fris 07-20-2008 05:25 PM

why not use isset?

Iron Fist 07-20-2008 05:28 PM

Quote:

Originally Posted by Hotrocket (Post 14483071)
Love watching coders one upping each other with their versions..and then the next...and the next...lol

Yup... see ... don't tell anyone, but if you time it just right and space out requests far enough so no one can piece them together, you could build an entire project for free through here... :) :1orglaugh

mrkris 07-20-2008 05:34 PM

This snippet makes many assumptions, so use it as a base. I haven't actually tested it, it's just a quick idea of a dirty way of doing it. There are literally hundreds of ways to include this. You can do what one person said and check to see if $_GET['id'] is present in an array of allowed types, you can check by db, etc.

Code:

include 'header.php';
$path = 'some/lib/path/' . (isset($_GET['id']) ? intval($_GET['id']) : 1) . '.php';
if (!file_exists($path)) {
    die("handle error goes here");
}
include $path;
include 'footer.php';


mrkris 07-20-2008 05:36 PM

Quote:

Originally Posted by sharphead (Post 14483556)
Yup... see ... don't tell anyone, but if you time it just right and space out requests far enough so no one can piece them together, you could build an entire project for free through here... :) :1orglaugh

I don't consider it a bad thing, I consider it an opportunity for a developer to see how other developers approach a problem with a solution. :2 cents:

Sands 07-20-2008 05:43 PM

Quote:

Originally Posted by mrkris (Post 14483572)
I don't consider it a bad thing, I consider it an opportunity for a developer to see how other developers approach a problem with a solution. :2 cents:

I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.

Bro Media - BANNED FOR LIFE 07-20-2008 05:54 PM

Quote:

Originally Posted by Sands (Post 14483584)
I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.

you liked it and you know it!

mrkris 07-20-2008 06:10 PM

Quote:

Originally Posted by Sands (Post 14483584)
I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.

Remind me to never pee in a urinal next to you :)

Sands 07-20-2008 06:38 PM

Quote:

Originally Posted by mrkris (Post 14483638)
Remind me to never pee in a urinal next to you :)

Yeah, wouldn't want you to feel inadequate. :winkwink:

mrkris 07-20-2008 08:23 PM

Quote:

Originally Posted by Sands (Post 14483689)
Yeah, wouldn't want you to feel inadequate. :winkwink:

sad but true :(

jimbona 07-20-2008 08:44 PM

Quote:

Originally Posted by potter (Post 14482952)
Ok, I definitely like the idea of checking that the id input is valid. However with jimbona's code snippet it only seems to load noid.php no matter what.

Just tested the code locally and works fine for me, make sure the id you are passing via get is ?ID= and not ?id=

do debugging by testing if the different vars such as $ID are being set correctly.

GrouchyAdmin 07-20-2008 09:47 PM

Quote:

Originally Posted by mrkris (Post 14483638)
Remind me to never pee in a urinal next to you :)

You pee sitting down, so the point is moot.


All times are GMT -7. The time now is 12:38 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123