if statement problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

if statement problem!

Post by bumple »

hi guys,

I am having a small problem right now...

This is how my current layout for my php page is, I will ask my question after the code:

[syntax=php]<?php
require 'db.php'; //database connect script, includes session_start() and $logged_in.

if($logged_in == 1) {?>

//LOTS OF COMMANDS HERE IF THE USER IS LOGGED IN
}
//NECESSARY HTML CODE FOR USERS THAT ARE BOTH LOGGED IN AND NOT LOGGED IN


<?php if($logged_in == 1) {?>
Welcome <?php echo $_SESSION['username'];?></strong></span>! Now that you are logged in, you are able to choose from the following:

//MEMBERS PAGE STUFF

<?php

} else { // if form hasn't been submitted

//LOGIN TEXTFIELDS

?>[/syntax]

So my question is this:

notice how I have two of the same if statements following one another (if($user_logged_in == 1))...I need the stuff in between both if statements to run whether the user is logged in or not, but it does not run the second if statement due to the fact that it has an identical one above it...IS THERE anything I can do to fix this? Or to somehow skip the necessary HTML in between?

Thanks guys!

-bumple
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

bump!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

just tidied your code up a bit and changed a few bits. See how you get on with it.

Code: Select all

<?php 
require 'db.php'; //database connect script, includes session_start() and $logged_in. 

if($logged_in == 1) { 

	//LOTS OF COMMANDS HERE IF THE USER IS LOGGED IN 

} 
?>

<!-- NECESSARY HTML CODE FOR USERS THAT ARE BOTH LOGGED IN AND NOT LOGGED IN -->


<?php

if($logged_in == 1) {

?> 

Welcome <?php echo $_SESSION['username']; ?>! Now that you are logged in, you are able to choose from the following: 

<?php

//MEMBERS PAGE STUFF

?> 

<?php 

} else { // if form hasn't been submitted 

//LOGIN TEXTFIELDS 

}

?>
Mark
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I think that you have also a designing problem: you are mixing too much HTML and PHP. That's why you are testing two times.
First you should create a function, which is generating the dynamic side of the page and not to mix that.
The code should looke like this:

<html>
<head></head>
<body>
<!—Some HTML that is generating the page -->
<? generate(); ?>
</body>
</html>

Code: Select all

<?php
function generate()
{
  // what you need
  if($issession)
  {
     ?>
     <!—generate inside area -->
     <?
  }
  else
  {
    ?>
    <!—Generate Login area + error code -->
    <?
  }
}
?>
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

Do you guys think it would be a better idea to just redirect the user to a new page upon clicking submit?

something like...

'if (user and password are correct) { then redirect to "login_success.php" }

else {echo 'your login information is incorrect, please try again'}'

If i do something like that, would it be more insecure? Or would that be a better idea? If I do this, it will be so much easier to place together...but if it isn't possible I guess I can just stick with the old version, THANKS SO MUCH GUYS!

-bumple.
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

bumpity bump!

-bumple!
Post Reply