7 Replies - 7827 Views - Last Post: 14 August 2008 - 07:27 AM Rate Topic: -----

#1 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

PHP Login requires page refresh to work!

Posted 13 August 2008 - 10:36 AM

Hi,

I'm new to PHP, and I'm trying to set up a test login page. Everything from validation to maintaining value in a session to logout works, but the page requires a refresh after a login to make the login form disappear. Likewise, after logging out, the page requires a refresh to display the login form again. I haven't been able to figure out the fault. Below is the code I had souped up from whatever knowledge I gained after a quick read of php sessions. So pardon any inefficieny and also pardon the lack of indentation, I am in a bit of a hurry :P

<?php

session_start();

?>

<html>
<head>
<title>Test Form</title>
</head>

<body>

  <?php

  if(!isset($_SESSION['is_logged_in']))
  {
  ?>
 <span>Login</span>

<br />
 
  <form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">

	 <fieldset>
	 <p>Username:</p>
	 
	 <input type="text" class="text" name="username" />

	 <p>Password:</p>
	 <input type="password" name="password" />
	 <input type="submit" value="Login" name="login" />
	 </fieldset>

  </form>

  <?php
  }
  if(isset($_POST['login']))
  {

$xml = simplexml_load_file('users.xml');

$username = $_POST['username'];
$password = $_POST['password'];

$count = 0;

foreach($xml->username as $currUser)
{
if($currUser == $username)
{
break;
}

$count++;
}

if($xml->active[$count] == 1)
{

if($password == $xml->password[$count])
{
$_SESSION['is_logged_in'] = 1;
echo "Valid user logged in!";
echo " Hi ".$xml->username[$count];
}

else
{
echo "Invalid username or password.";
}

}

if($xml->active[$count] == 0)
{
echo "Username is inactive or does not exist.";
}
  }
  ?>

<?php
if(isset($_POST['logout']))
{
session_unset($_SESSION['is_logged_in']);
session_destroy();
echo "logged out";
}
?>

<?php
if(isset($_SESSION['is_logged_in']))
{
?>
<form method = "post" action="<?=$_SERVER['PHP_SELF']; ?>">
<input type ="submit" name="logout" value="logout" />
</form>
<?php
}
?>

	 <br />

</body>
</html>



NOTE: I'm using user details from an XML file for this purpose. Am I doing something wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Login requires page refresh to work!

#2 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: PHP Login requires page refresh to work!

Posted 13 August 2008 - 01:33 PM

Hi,

I'm in a rush so may have missed something but to me it seems you need to handle requests for login and logout at the top of the script (or at least execute login/logout code first).

I.E.


// Session start and stuff but NO output

$loggedIn = isset($_SESSION['loggedIn']);

if (isset($_POST['login'])) {
    // check login stuff and then set login and set session stuff
    $loggedIn= true;
} else if (isset($_GET['logout'])) {
    // Unset session stuff 
    $loggedIn = false;
}

// Now do code to display
if ($loggedIn) {
   // Display stuff for user
} else {
   // Display login form
}



Also storing in an XML isn't the best I guess cos if people find out what it is then they can find out user names and passwrord. Also at least store the password hash as opposed to the password:
// In creating user code and checking login password
$password = md5($_POST['password'])



Hope that helps, written in a rush though so maybe syntax errors!

[EDIT]
Looking back, I didn't really explain why; when you displayed the login form at the top it was before you had unset SESSION login details so at that point in the script the user is still logged in so it won't display the form, later on you then log out. On a refresh though the SESSION stuff is now unset so will display form.

This post has been edited by eddieboy665: 13 August 2008 - 01:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: PHP Login requires page refresh to work!

Posted 13 August 2008 - 11:12 PM

Hi eddieboy665,

Yes, that worked! I guess I have been complicating things with my code. I guess that can happen when you have to learn PHP, ASP.NET and C all in one go :P. About storing details in an xml file... yeah, I am aware of the security problems, but its just a homework thing, and besides my tutor wants me to use xml or text files and I figured xml files are easier to manipulate than text files. Yep, I am thinking of hashing the passwords! Thank you for your time and help mate! :D
Was This Post Helpful? 0
  • +
  • -

#4 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: PHP Login requires page refresh to work!

Posted 14 August 2008 - 06:27 AM

EDIT 2: Grrr, it seems every time I fix something, another problem pops up! This is driving me crazy! I fixed my previous problem as well, apparently it had something to do with simplexml_load_file() and setting values from it to $_SESSION, I had to cast the value to a string in order for it to work. Might be useful info for anyone working with simpleXML. Ref: http://bugs.php.net/bug.php?id=33142 (check the bottom most post).


EDIT: Oh scratch that, I made it work. I hadn't registered the session variable. But registering it (in index.php for eg.) and navigating to another page (profile.php for eg.) throws this error message on top of the profile.php page, although it displays fine and the user is still logged in:

Warning: session_start() [function.session-start]: Node no longer exists in (file path) on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at **file path**) in (file path) on line 2

Well, now I have a new problem. All of a sudden, the sessions stopped working! I now have a few pages with the session codes in them, and the login on each page works fine. But when I navigate to another page, it says the user is no logged in (I.E. the login form is displayed). I have checked that session_start() is at the top of every page, and that nothing is sent before that piece of code. What might be the problem?

This post has been edited by agent_logic: 14 August 2008 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: PHP Login requires page refresh to work!

Posted 14 August 2008 - 06:57 AM

is session_start() being called twice in one page?
Was This Post Helpful? 0
  • +
  • -

#6 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: PHP Login requires page refresh to work!

Posted 14 August 2008 - 07:07 AM

View Postpemcconnell, on 14 Aug, 2008 - 06:57 AM, said:

is session_start() being called twice in one page?

No mate, it was a conflict with simpleXML like I explained. I'm in a hurry to finish off my homework so I am posting things in a hurry before googling for a solution first, which probably might not be found in good spirit by you guys! :(

Thanks for taking your time to reply though! :D

This post has been edited by agent_logic: 14 August 2008 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#7 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: PHP Login requires page refresh to work!

Posted 14 August 2008 - 07:17 AM

Sorry dude - never used simpleXML before :(
Was This Post Helpful? 0
  • +
  • -

#8 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: PHP Login requires page refresh to work!

Posted 14 August 2008 - 07:27 AM

Me neither, I'm relatively new to PHP. But I'm glad my googling "skillez" (read: luck :P) are still intact! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1