php log in system 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
dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

php log in system problem

Post by dourvas »

hallo,
I have implemented a loging system using sessions. It was working fine till last week. Suddenly some problems occered. I dont remember me making any changes to the code. so, i can not figure out what caused these problems...

To be more specific, a user logs in through a form (of course), a query searches my mysql database and if the username and password exists the system executes the code:

Code: Select all

session_regenerate_id();

			session_register("authenticated"); 
			$member = mysql_fetch_assoc($result);//the query executes ok

			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
			$_SESSION['SESS_CLASS'] = $member['class'];
	        	$_SESSION['SESS_FATHERSNAME'] = $member['fathersname'];
			$_SESSION['SESS_LOGIN'] = $member['login'];
	
			session_write_close();
Note that i have started the session earlier.

Just as simple as that. So, if the username and password are ok i hide the form and i echo the message hallo username...
That code was and still is workng ok but not all of the times!!!! Many times i have to refresh the page more than once in order to see the hallo message and dissapear the login form which are the proofs for my succesfull login..
IT is very odd.
I am thinking that it is a server side problem because i did not make any code changes. I can not thing anything that i should try in order to fix it. Do u thing i should communicate with the company that hosts my page? I dont really know what to ask them for.......
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php log in system problem

Post by social_experiment »

If the php version was updated to 6.0.0, session_register() might not be registering your variables. Ask your hosting provider about any updates?
The Manual wrote:This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
Use $_SESSION like you did with other session variables.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

Re: php log in system problem

Post by dourvas »

Thanks for your quick answer.

I thought about it. But as i said before, the login system works most of the times. Sometimes and only sometimes i have to refresh the browser(2-3 times) in order to work!!!
i tried the code without session_register("authenticated");
the same!!
:banghead:

let me say somethings about the code.

In order to login i search into a database for the username and password entered in a form. if the data are ok i
session_register("authenticated");
and i declare some session variables as i wrote to my first post.

When i want to check if a user is logged in at the very first of the page i start the session and then i use that code:

Code: Select all

if(!session_is_registered(authenticated))
 {
echo "welcome";
.........
}
or

Code: Select all

if(isset($_SESSION['SESS_LAST_NAME']))
 {
      echo "welcome";
.........
}
i use that code to many pages.
Most of the times the login is fine. Sometimes, (i can uderstand when and why) i have to manually refresh a page to see the welcome message
u can check it yourself
http://gym-kassiop.ker.sch.gr
username: anonymous
password: anonymous
just tour at the pages of the central menu (Home, teachers, students, links..)
if u try to log in, the form must dissapear and the message welcome must show. if it wont just refresh! it will show. Then if you go to 1-2 pages of the above you may see the welcome message. Probably sometime it will not show. Then refresh. 2-3 times is needed sometimes.

What should i do?????
i am going crazy
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php log in system problem

Post by social_experiment »

Odd indeed. Another thing i read in the manual was this, maybe it's related to your situation.
The Manual wrote:Caution
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

Re: php log in system problem

Post by dourvas »

i ve noticed it. so i erase that piece of code. But nothing...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php log in system problem

Post by social_experiment »

What does the code on your auth page look like?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

Re: php log in system problem

Post by dourvas »

Every page begins with this piece of code

Code: Select all

<?php
 error_reporting(E_ALL ^ E_NOTICE);
 ?>
<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
	xmlns="http://www.w3.org/1999/xhtml"
	lang="en"
	xml:lang="en"
><head>

<meta
	http-equiv="Content-Type"
	content="text/html; charset=windows-1253"
/>

<link
	type="text/css"
	rel="stylesheet"
	href="screen.css"
	media="screen,projection,tv"
/>

<script	type="text/javascript"
	src="library.js"
></script>

<title>
		school
</title>

</head>

<body>
<div id="pageWrapper">

  <h1>
	(call image header)
	  	<span></span>
	</h1>

<div class="mainmenu">

<ul>
      (call main menu)
          <?php
    
 if(!isset($_SESSION['SESS_LAST_NAME']))// a piece of main menu depends whether the user is logged in or not
 {
      echo   '<li class="li_hc"><a href="blog/blog1.php" target="_self" >choice 1(Blog)</a><ul class="ul_ch">';
          }
  else
  {
         echo '<li class="li_hc"><a href="blog/blog.php" target="_self" >choice 2(Blog)</a><ul class="ul_ch">';
         }
?>
</ul>


</div>

<div id="headerLogin">
<p>
	<?php
//	if(session_is_registered(authenticated))
if (isset($_SESSION['SESS_LAST_NAME']))
{
 echo "Welcome". $_SESSION['SESS_LAST_NAME'];
  echo "<br>";
 echo "<a href='login/logout.php'>logout</a>"; 
echo "<br>";
}
else
{
?>	
		
		<form method="post"
		action="login/login-exec.php"
		id="headerLogin">
		<div>
		
			<label for="login">usename:</label>
			<input
				name="login"
				id="login"
				type="text"
				size="12"
			/>
			<label for="password">pasw:</label>
			<input
				id="password"
				name="password"
				type="password"
				size="12"
			/>
			<input 
				type="submit"
				name="Submit"
				class="submit"
				value="Ïê"
			/>
		<!-- headerLogin --></div>
		
		<br>
 
<?php
}
?>
</p>
</div>


dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

Re: php log in system problem

Post by dourvas »

And the mystery goes on...

I thought that maybe the session expires (timeout) or that something were going wrong with the session id.

So i echoed the session id just to see if it changes when it looks that i am logged out before i refresh the page.
i used that code

Code: Select all

$a = session_id();
if(empty($a)) session_start();
//echo "SID: ".SID."<br>session_id(): ".session_id()."<br>COOKIE: ".$_COOKIE["PHPSESSID"];
the minute i used that code the problem seems to be solved!! I had not had to refresh any pages for the last hour. It looks like everything is ok!!!! :drunk:

i am sure that something else happened and the problem dissapeared. But i did not tried something else.
Maybe from the server side... Idont know. Is it possible? if they cleaned up some session garbage that probably had caused the problem.. is that possible??

if the problem cames back i wont know how to handle it..

What do u propose that i should do?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php log in system problem

Post by social_experiment »

dourvas wrote:Maybe from the server side... Idont know. Is it possible? if they cleaned up some session garbage that probably had caused the problem.. is that possible??
Truth is stranger than fiction. Find out from your hosting provider if they did anything (updates, maintennance) on the server and inform them of your issues. If the problem arises again you will have a point to start working from in solving the situation.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: php log in system problem

Post by John Cartwright »

Code: Select all

<?php
 error_reporting(E_ALL ^ E_NOTICE);
 ?>
<?php
session_start();
?>
This could potentially cause an error depending on your text editor. You want NO whitespace between your opening tag, and any header calls. I.e., should be

Code: Select all

<?php
error_reporting(E_ALL ^ E_NOTICE);

session_start();
?>
Post Reply