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
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.
Link to comment
Share on other sites

I prefer to use the else elsif statement but some users like to use the switch statement all down to your programming prefrence ok.

most users will post all types of ways to get the result wanted under there own programming prefrence ok.

good luck.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.