Web Hosting Talk







View Full Version : [PHP] Using simple 'if' and 'else' statements; they just don't work together!


jonathanbull
12-26-2004, 09:31 PM
Hi there,

I am using a very simple login script to log a user into my website. The data is stored in a simple text file.

To log in a user, I'm using sessions. The following is a very small part of my script...


$formatbasic = "$username1|$password1|basic|";
$formatadvanced = "$username2|$password2|advanced|";
$formatsuper = "$username3|$password3|super|";

if (eregi($formatbasic , $fileinfo)) { $_SESSION['basic'] = $username; }
if (eregi($formatadvanced , $fileinfo)) { $_SESSION['advanced'] = $username; }
if (eregi($formatsuper , $fileinfo)) { $_SESSION['super'] = $username; }
header("Location: userlogin/login.php");

Now, this should create a different session for each individual member status (basic, advanced, or super). For example, if I logged in as username1, a session would be created in the name of 'basic'. If I logged in as username 2, a session would be created in the name of 'advanced', and so on...

The problem lies with the fact that I need to place 'else' statements between each line. I have tried placing the } else { tag before line 2 and 3, but this just causes the page to fail loading.

Perhaps I'm approaching this from totally the wrong way. What seems like an easy piece of coding has totally baffled me.


Anyway, I hope I've made myself clear enough and that I've included enough of the code. If anybody could point me in the right direction I'd be very grateful. Any help is greatly appreciated. :)

azizny
12-26-2004, 09:38 PM
The code should work ok... but you can always check for the session if it is still null on the second and third check..

You can also do:

if ( tatat){
do this;
} else {
if (tatata3){
do this2;
} else {
do this3;
}
}

You can also approach it with the elseif or switch statement approach, whichever suits you best...

Peace,

deadserious
12-26-2004, 09:39 PM
Can't really tell for sure without seeing more of the code, but have you tried elseif?


if (this)
{
one
}
elseif (this)
{
two
}
else
{
three
}

jonathanbull
12-27-2004, 12:03 AM
Thank you very much azizny and dead serious.

Those helpful comments were exactly what I was looking for. The elseif statement works perfectly.

Thanks again!