Page 1 of 1

Help with simple if then statement

Posted: Fri Dec 05, 2008 11:07 am
by Remix919
Hey guys, I used to know a lot about php code and wrote several games, but haven't used php in quite some years. I was recently building a website and just needed a simple if then statement, here's my code:

Code: Select all

 
<?
if($view == "") {
$view = "main";
}
include "$view.php";
?>
 
The problem is that it'll replace the $view variable and display the main.php easily, but when I use the url like index.php?view=somepage, it won't display it, it won't even show up the $view variable when I echo it. I was just wondering if there's been some major changes to PHP lately to the point where I can't pass a variable through a URL? I have PHP ver 5.2.5.

Thanks in advance for the help! :)

Re: Help with simple if then statement

Posted: Fri Dec 05, 2008 11:13 am
by watson516
Remix919 wrote:Hey guys, I used to know a lot about php code and wrote several games, but haven't used php in quite some years. I was recently building a website and just needed a simple if then statement, here's my code:

Code: Select all

 
<?
if($view == "") {
$view = "main";
}
include "$view.php";
?>
 
The problem is that it'll replace the $view variable and display the main.php easily, but when I use the url like index.php?view=somepage, it won't display it, it won't even show up the $view variable when I echo it. I was just wondering if there's been some major changes to PHP lately to the point where I can't pass a variable through a URL? I have PHP ver 5.2.5.

Thanks in advance for the help! :)

Code: Select all

 
<?php
$view=$_GET['view'];
if($view=="")
{
    $view="main";
}
 
include($view.".php");
?>
 
Something like that I could think.

I believe the ability to reference variables passed via GET with just the variable name is disabled by default in newer version of php. Then again, I could be completely wrong.

Re: Help with simple if then statement

Posted: Fri Dec 05, 2008 11:18 am
by Remix919
Thanks! that code worked perfectly, so apparently now we have to use the GET command now eh, pfft...I don't suppose you know if there's anyway to turn that off so I can get variables directly from the URL without having to do that?

Re: Help with simple if then statement

Posted: Fri Dec 05, 2008 11:23 am
by watson516
Somewhere in your ini file. Not sure where, but its in there.

Re: Help with simple if then statement

Posted: Fri Dec 05, 2008 12:28 pm
by Peter Anselmo
The ini setting watson referred to is register_globals

PHP 5 changed it so you must specify _GET by default. This greatly improves the security of your scripts and I (and many other people) would recommend not turning it off, but rather embracing it in your code. Also, you'll soon find it a boon to be able to distinguish GET, POST, SESSION, and COOKIE explicitly. Also, most servers require it now by default, so your code will be much more portable and re-usable if you use it.