Solved

php Session issues

Posted on 2011-09-07
7
292 Views
Last Modified: 2012-08-13
I am trying to control access to some web site "back end" pages via session & session variables.

I am using a technique I have used on other sites & it has always worked perfectly, until now.

Maybe there is a change in php 5?

See attached php files.

The chk_login.php is used to check the validity of the login & go to the admin menu. The admin_menu.php is self-explanatory.

But they don't work. After the timeout, the admin_menu page is just a blank page with the <!DocTYPE and <html tags at the top, rest of page completely blank.

What's wrong?

Thanks
admin-menu.php
chk-login.php
0
Comment
Question by:Richard Korts
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36497775
The correct design pattern for PHP client authentication is available in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Please read it over and have a look at the code samples.  It is intentionally simplistic, but it is the foundation of "how it's done."
0
 
LVL 27

Expert Comment

by:yodercm
ID: 36497810
Make sure the webpages are all under the same domain level directory.  Session variables do not persist across domains.
0
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 500 total points
ID: 36497837
potential problems for the admin-menu.php page
you cannot have any type of output to the browser before you do the session_start(), try moving that to before the doctype declaration. It is the same with any header information from php.
Check your ini file to see if you have short tags enabled otherwise you have to start php scripting with <?php not just <?
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36497842
Here is the chk-login.php script annotated with some comments.  Error checking is important when you are running queries.  MySQL is not a black box -- it can and will fail for reasons that are outside of your control.  You would want to trap these failures and take appropriate action.
<? // THIS USES THE SHORT-OPEN TAG.  SUGGEST YOU CONVERT TO THE FULL TAG LIKE <?php

// THIS NEEDS TO BE THE FIRST LINE OF EVERY SCRIPT
error_reporting(E_ALL);

// set up database
$Host = "db380492857.db.1and1.com";
$User = "dbo380492857";
$Password = "abcxyz";
$DBName = "db380492857";

// THE FOLLOWING INSTRUCTION CAN FAIL, AND MUST BE TESTED FOR SUCCESS
$Link = mysql_connect ($Host, $User, $Password);

// THE FIELDS HERE NEED TO BE ESCAPED
$qry = "SELECT * from admin where code = '" . $_POST['uc'] . "' and password = '" . $_POST['pwd'] . "'";

// THIS FUNCTION IS DEPRECATED - CHANGE IT. http://php.net/manual/en/function.mysql-db-query.php
$res = mysql_db_query ($DBName, $qry, $Link);

// HOW DO YOU KNOW WHAT VALUE IN IN $res?  YOU HAVE TO TEST IT BEFORE YOU TRUST IT IN ANOTHER FUNCTION
$n = mysql_num_rows($res);

// WHAT WOULD YOU DO IF $n == 3?  MAYBE YOU NEED A LIMIT IN THE QUERY
if ($n == 0) {
	header("Location: login.php?bad=1");
	exit;
}

// THE SESSION IS NOT STARTED UNLESS THE LOGIN IS SUCCESSFUL?  THAT IS A RECIPE FOR CONFUSION
session_start();
$_SESSION['vavusr'] = $_POST['pwd'];
$_SESSION['user'] = $_POST['uc'];
$_SESSION['alast_used'] = time();	
header("Location: admin_menu.php");
exit;

// THE ZEND CODING STANDARD RECOMMENDS ELIMINATING THE CLOSING PHP TAG. OMIT IT.
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36497867
Regarding this, Session variables do not persist across domains.

I think the issue might be across sub-domains?  That may or may not be true.  If you need session variables to persist across sub-domains, it is easy enough to make it happen.  You just have to set the session cookie yourself.  If you do not set the session cookie yourself, the PHP session handler will set a cookie that does not persist across sub-domains.  No cookie persists across domains - cookies are domain-specific.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36497924
Same general comments apply to admin-menu.php -- you need to use error_reporting(E_ALL) to see what might be going on.  You need to replace the deprecated code, etc.  You might want to ask yourself why you are testing $tdiff and mucking up the client session.  PHP has its own session timeout mechanism.  I think I would use that instead or trying to write my own.
0
 

Author Comment

by:Richard Korts
ID: 36503554
To Ray_Paseur:

Thank you for all your comments & your impressive tutorial on this general subject.

However, I have been successfully using the technique I described on other sites.

As it turns out, the comment by haloexpertsexchange:solved the problem, moving the session_start to the beginning resolved the issue.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question