Author Topic: PHP Question  (Read 3644 times)

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
PHP Question
« on: March 10, 2008, 11:17:54 PM »
Hey,

I put my website on hold for a while and Im finally getting back to it...

Users can create accounts and login.  Im having an issue when they login. I have the form for the username and password. If their login data is correct I want to direct them to another page and change the header (which would now have a logout button instead of login), but I cant call the header() function since the page has already been displayed. How do I get around this?

Thanks

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP Question
« Reply #1 on: March 11, 2008, 09:20:01 AM »
As long as you do your verification of the login details before you display the page, you can use header() to redirect:

Code: [Select]
<?php
/* verify username and password */
if($username == valid and $password == $valid)
{
&
#160;  $_SESSION['logged_in'] = true;
&#160;  header(blah blah);
}
else
{
&
#160; &#160; $errmsg = "Incorrect login details";
}

?>

<html>
<body>
// show form again

And then on all pages you do a simple check of your session variable (or cookies if you prefer that route) and depending on the value stored for the "logged_in" variable, you either show the login or logout message.

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: PHP Question
« Reply #2 on: March 11, 2008, 10:22:41 AM »
you can also buffer output.

look at the documentation for ob_start(), ob_end_flush(), etc on php.net

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: PHP Question
« Reply #3 on: March 11, 2008, 10:47:29 AM »
What about this scenario.

The user isnt logged in, they come to the login screen for the first time.

The page is already displayed, with the login form. When they login I cant use the header() function because the page is already displayed. I would need to display all the header information in order to disaply the form.

I will look into those functions.

Am I not following correctly?

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP Question
« Reply #4 on: March 11, 2008, 10:56:09 AM »

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP Question
« Reply #5 on: March 11, 2008, 11:48:09 AM »
The page is already displayed, with the login form. When they login I cant use the header() function because the page is already displayed. I would need to display all the header information in order to disaply the form.
You're missing the point.  You need to process the login details before you display anything or use output buffering.  Just check your "logged in" variable, which should be stored in a session var or cookie.

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: PHP Question
« Reply #6 on: March 11, 2008, 11:55:21 AM »
When the user comes to the page, I can check the session variable and send them elsewhere if necessary.

However, if you come to a page and are not logged in, there is a form to input your username and password.

You click the Login button, at this point I cannot use the header command since the form is already displayed. After the login I want to send them to another page.

I output login information based on the user's last login, but for some reason to get the correct data outputted I have to manuall refresh the page. When I get home maybe posting some code will be easier to explain the issue.

Thanks!

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP Question
« Reply #7 on: March 11, 2008, 12:08:22 PM »
I think you're missing the point.  Your origional question was "how do I display the 'logout' link when the user is logged in and vice versa?".

Maybe some code will help:
Code: [Select]
<?php
if($pg->logged_in == 1)
{
?>


<div>
    <p class="sideBarTitle">Editing Options</p>
    <ul>
    <li><a href="index.php?req=welcome">&rsaquo; Home</a></li>
    <?php
    
$query "SELECT * FROM modules WHERE mod_status = 1 AND menu_show = '1' ORDER BY mod_display";
$result $ehandle->update_query($query);
if($result && mysql_num_rows($result))    
{
  while($row mysql_fetch_array($result))
  {
    extract($row);
echo '<li><a href="index.php?req='.$mod_name.'">&rsaquo; '.$mod_display.'</a></li>';
}
}
?>

    </ul>
  </div>
<?php
}
?>

 
  <div>
        <p class="sideBarTitle">Additional Info</p>
        <ul>
          <li><a href="index.php?req=help">&rsaquo; Help</a></li>
          <li><a href="index.php?req=contact">&rsaquo; Contacts</a></li>
        </ul>
      </div>

<?php
if($_SESSION['level'] > && $pg->logged_in == 1)
{
?>

<div>
        <p class="sideBarTitle">Admin</p>
        <ul>
          <li><a href="index.php?req=config">&rsaquo; Admin Panel</a></li>
          <li><a href="index.php?req=modules">&rsaquo; Modules</a></li>
          <li><a href="index.php?req=users">&rsaquo; Users</a></li>
        </ul>
    </div>
<?php
}
?>


<?php
if($pg->logged_in == 1)
{
?>

<div>
        <p class="sideBarTitle">Logged in:</p>
        <span class="sideBarText">
        <img src="images/user.png" alt="user">&nbsp;&nbsp;<?=$_SESSION['user']?>
        </span>
      </div>
<?php
}
elseif(
$pg->page_type != 'login' && $pg->page_type != 'loginuser' )
{
?>

<div>
        <p class="sideBarTitle">Login:</p>
        <span class="sideBarText">
<form action="index.php?req=loginuser" method="post">

Username:&nbsp;&nbsp;
<?php isset($username) ? $value "value=$username$value ""?>
<input type="text" name="user" size="15" <?=$value?> /><br/>
Password:&nbsp;&nbsp;
<?php isset($password) ? $value " value=$password$value ""?>
<input type="password" name="pass" size="15" <?=$value?> /><br/>

<a href="index.php?req=forgotpass">[Forgot User/Pass]</a><br/>
<a href="index.php?req=signup&amp;signup=1">[Register]</a><br/><br/>

<input type="submit" value="Login" name="login" />&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" />

</form>
    </span>
      </div>

<?php
}
?>

I then have 2 routines in my "page builder" class... one that does an auto-login (verifies details on every page load from cookie or session) and one that is a manual login from the login form.

Someone is probably going to look at the above code and laugh because I did something stupid... but whatever.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP Question
« Reply #8 on: March 11, 2008, 12:28:05 PM »
When the user comes to the page, I can check the session variable and send them elsewhere if necessary.

However, if you come to a page and are not logged in, there is a form to input your username and password.

You click the Login button, at this point I cannot use the header command since the form is already displayed. After the login I want to send them to another page.

I output login information based on the user's last login, but for some reason to get the correct data outputted I have to manuall refresh the page. When I get home maybe posting some code will be easier to explain the issue.

Thanks!

Unless you are using something like an AJAX login then once the user submits the forum a new request will be made to the server.  You can then process the login and display or redirect as needed.