Yikes... lot of disinformation in this thread. Here are some comments to set everyone straight:
Joshua F;10983230 wrote:First, I see you used <? and not <?php. The difference is the PHP compatibility, <?php has more features then <? does. That could be the problem.
Incorrect; neither of the tags has any more "features" than the other since they both have at most one feature: they get the PHP interpreter into PHP mode.
The only difference is that '<?' is often disabled for compatibility with XML, so to write portable code you should always use the full '<?php' tag and never use the '<?' or '<?=' short-open tags.
Joshua F;10983235 wrote:<?php
ob_start();
session_start();
$action = $_GET['p'];
$actions_array = array('','game','register','error','logon','banned','logout');
?>
Use that as your top code, to make it correct.
Incorrect, IMHO. Why use [man]ob_start/man? It's unnecessary as it adds extra load to the server for no reason. Get rid of it. (Hint: No, it is not a solution to the 'Headers already sent' errors - it's a cheap workaround. 😉)
Joshua F;10983235 wrote:Also, just remember Undefined index means that the variable isn't found. So when you're registering the session, you must not of typed it correctly.
Not really; it just means that there is no element in the array defined by the key/index given. However, with external data, this is to be expected and accounted for...
brroot;10983237 wrote:change if($SESSION['username']) to if($SESSION) and now it works perfectly with no annoying notices
Not really - you just swept things under the rug and pretended as if they were fixed. The "annoying notices" are gone because you aren't checking the same condition that you were before.
Instead, the answer is to use [man]empty/man or [man]isset/man as is suggested below.
Joshua F;10983238 wrote:Also, I believe you should try this.
if(isset($_SESSION['username'])){
Ah, finally - we're at the crux of the issue at hand.
External data should never be referenced without first verifying that it exists (e.g. using [man]isset/man or [man]empty/man). For example, this:
$action = $_GET['p'];
is poorly written IMHO and should instead look something more like this:
$action = isset($_GET['p']) ? $_GET['p'] : NULL;
(which uses the ternary operator to set a default value if the external data doesn't exist).
EDIT: Also, don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu above.