Jump to content

[SOLVED] If Statement Help


SelfObscurity

Recommended Posts

Hello everyone.  I am trying to do some basic scripting, and I'm finding out it isn't so basic, for me at least.

I'm trying to create a page, that would be constructed of if statements, for example...

index.php
index.php?id=one
index.php?id=two

I thought I had an idea of how to make that happen, but it doesn't work.  Here is my code...

if (id == ['one'])
include ("page1.php");

elseif (id == ['two'])
include ("page2.php");

else
include ("main.php");
Link to comment
https://forums.phpfreaks.com/topic/14056-solved-if-statement-help/
Share on other sites

Basically, you're probably forgetting some basic PHP rules because you're new to PHP.

1) All variables start with a dollar sign: $.
2) If you want to get a variable from the address like "page?var=3" then you should use this:
[code]$var = $_GET['var'];[/code]
Inside $_GET['NAME'] replace NAME with the name of the variable in the address string.

3) Try using brackets, which will stop errors when you use longer if code blocks, e.g:
[code]
if ($id == 'one')
{
  include ("page1.php");
}
elseif ($id == 'two')
{
  include ("page2.php");
}
else
{
  include ("main.php");
}
[/code]

Hope it helps.
i would also like to mention here that even though those methods work, it opens up a security risk for people to inject their own pages into your website.  You should setup your code to only allow specific values from your get variable. here is an example of what you should be doing:
[code]
<?php
$allowable_page = array('home','about','faq');

$id = $_GET['id'];
if((isset($id)) && in_array($id,$allowable_page)) {
include ("http://www.yoursite.com/".$id.".php");
} else {
include ("http://www.yoursite.com/index.php");
}
?>
[/code]
there are 2 main elements to this code.

a) the list of allowable values as set in $allowable_page. 
b) explicitly using the specific url in your include, instead of a relative path

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.