473,793 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems using sessions

9 New Member
Hi, I'm trying to use sessions to create a login system to administer a website - add new info to database,etc but i'm running into some problems.

The first page login.php seems to work okay, it authenticates the user then takes them to the next page update_db.php but when i get to this page, it seems all the session data is lost :icon_conf

login.php
[PHP]<?php
session_start() ; // we must never forget to start the session

$errorMessage = ''; // set this blank to check back the value later
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword']))
{
//Include our login information
include_once('d b_login.php');

// Let's show all errors
error_reporting (E_ALL);

// initiate connection to the database
$connection = mysql_connect($ db_host, $db_username, $db_password);
if (!$connection)
die("<strong>Co uld not connect to MySQL: </strong> <br />" . mysql_error());

// select the ecflora database
if (!mysql_select_ db($db_database ))
die ("<strong>Co uld not select the database: </strong> <br />". mysql_error());

$userId = mysql_real_esca pe_string($_POS T['txtUserId']);
$password = mysql_real_esca pe_string($_POS T['txtPassword']);

// check if the user id and password combination exist in database
$sql = "SELECT user_id FROM auth_user WHERE user_id = '".$userId." ' AND user_password = PASSWORD('".$pa ssword."')";

$result = mysql_query($sq l) or die('Query failed. '. mysql_error());

if (mysql_num_rows ($result) == 1) // a returned row indicates the password was valid
{
// the user id and password match, set the session
$_SESSION['db_is_logged_i n'] = true;

// store the username as well, just to be user friendly
$_SESSION['username'] = $userId;

// after login we move to the update page
header('Locatio n: update_db.php') ;
exit; // Make sure that code below does not get executed when we redirect
}
else
$errorMessage = 'Sorry, wrong user id / password';

// free the result set memory
mysql_free_resu lt($result);

//close the connection
mysql_close($co nnection);
}
?>[/PHP]

update_db.php
[PHP]<?php
session_start() ; // we must never forget to start the session

// check if the user is logged in and authorised to view this page
if(!isset($_SES SION['db_is_logged_i n']) || $_SESSION['db_is_logged_i n'] != true)
{
header("Locatio n: login.php"); /* Redirect browser */
exit;
}

$greeting = '';
if(isset($_SESS ION['username']))
$greeting = "Logged in as ". $_SESSION['username']. " (<a href='login.php ' onclick='logout User()'>Logout</a>)";

//Include our login information
include_once('d b_login.php');

// Let's show all errors
error_reporting (E_ALL);

// initiate connection to the database
$connection = mysql_connect($ db_host, $db_username, $db_password);
if (!$connection)
die("<strong>Co uld not connect to MySQL: </strong> <br />" . mysql_error());

// select the ecflora database
if (!mysql_select_ db("ecflora_db" ))
die ("<strong>Co uld not select the database: </strong> <br />". mysql_error());
?>[/PHP]

I tried to print_r the SESSIONS array and it was empty on update_db.php, any ideas what could be wrong?
Mar 7 '08 #1
5 1552
ronverdonk
4,258 Recognized Expert Specialist
Works fine here. I ran your code, with some change to the userid/password selection, and it runs without error. I can display the $_SESSION array and it contains all expected value. I would, however, not use true and false but 1 and 0 (but that is not important) and the best test for booleans is not $a!=true, but $a!==true.

So what can be the cause?
Another thing: what is your PHP version?

Ronald
Mar 8 '08 #2
dipperdan
9 New Member
ok i'm not sure if this is supposed to happen this way but the session data prints ok when i have it in that php block i posted (which comes before any html headers btw), but what i was doin before was trying to output inside a div, and nothing was printing in the array, i have no idea why that would happen this :(

btw i'm using XAMMP 1.6.6a with PHP version 5.2.5

EDIT: I found out why that was happening...

my logout function
Expand|Select|Wrap|Line Numbers
  1. function logoutUser()
  2.     {
  3.         <?php
  4.             // if the user is logged in, unset the session
  5.             //if (isset($_SESSION['db_is_logged_in']))
  6. //            {
  7. //                session_unset() ;
  8. //                session_destroy();
  9.                 //unset($_SESSION['db_is_logged_in']);
  10. //            }
  11.         ?>
  12.     }
the $_SESSION['db_is_logged_i n'] was being evaluated true and the session was being destroyed my bad heheheh, how could i do a better/safer logout function though?
Mar 10 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
sometimes you might have problems even if using both session_unset and session_destroy . You have to clear the $_SESSION array. I got it working this way:
Expand|Select|Wrap|Line Numbers
  1. session_unset();
  2. session_destroy();
  3. $_SESSION = array();
This is a user log entry from the php documentation. Maybe it's worth doping this, just to be sure.

Ronald
Mar 10 '08 #4
dipperdan
9 New Member
This is a user log entry from the php documentation. Maybe it's worth doping this, just to be sure.

Ronald
ok thanks, that seems to be workin, i modified it a bit so when you click to logout u get sent to login.php?do=lo gout and the session gets destroyed then
Mar 10 '08 #5
ronverdonk
4,258 Recognized Expert Specialist
Okay. Glad it works for you.

See you around.

Ronald
Mar 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2282
by: Steve Holdoway | last post by:
Hi there, Is there any way of defining a variable who lives as long as the browser stays active? I did some initial tests, and found that the session variables did this, but now I've developed some software that requires this, I find that the session id ( for example ) changes every time you change url to a new server. I think it may have been me not doing what I should when I set up virtual servers to test this ): What I'm trying to...
2
1730
by: John | last post by:
Hello I have created login pages but am having problems when I test them for my site. When I type all the registration info in it accepts it no problem. When I try and sign in though and then try and access the restricted pages it takes me back to the login page. The local root folder for the site is:
0
4663
by: Nitec Dev | last post by:
Our setup: server1 ASP database running on IIS5, Outlook 2000 and SQL 2000 on Windows 2000. Server2 runs Windows 2000 with Exchange 2000 using CDO 1.2 MAPI Sessions. Clients use IE5/6 and had Outlook 2000 installed. Upgraded to ASP running on IIS6, Outlook 2000 and SQL 2000 on Windows 2003. Server2 runs windows 2003 with Exchange 2003 using CDO 1.2 MAPI Sessions. Clients use IE5/6 and have Outlook 2000/2003 installed.
7
1873
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long as 2300 ms to complete. This is fairly rare, but the test code below will definitely show it. Somehow, I must not have my design right. The idea of this code is that I do two different types of processing ( 1-starting and 2-ending) based on...
3
3539
by: Scott | last post by:
Hello, we are having problems displaying non-aspx files (images, style sheets) since we have upgraded to the 1.1 framework when using a cookieless session (sessionID in the url). Check out our file system set up below. Now, in an aspx page, we set as the "src" of our images something like "../images/animage.jpg". If the current actual url in the browser is something like http://server/webapp/(someSessionID)/forms/form1.aspx
4
1618
by: Charles | last post by:
I am still having problems with sessions crossing over from other sessions. What I mean by this is that Mary may get just some or all of Renae’s information. I think that what is going on with this problem is the way that I have the solution setup I have this .NET CEmployees class that I am storing into a session object. The sessions are stored using SQL Server. I also have this ActiveX DLL file created in VB6. The ActiveX DLL file has...
3
1392
by: Efrain Flores | last post by:
Hi I am experimenting session problems in ASP.NET. I am developing a data entry system composed of 3 different aspx pages. The values entered on the form fields are stored in sesion variables. These values are saved when the user presses the Next page button. This applies for the 3 pages. Once they are complete, I empty the values of the sessions into the database (Oracle). The users are telling me that some of the data is missing in...
20
1703
by: John | last post by:
I have a page that requests an index number from the user using a form. The submit then calls itself and a php lookup table determines the web page required and then does a jump. echo "<script>window.location.href='$a'</script>"; Details of the page are passed in the address line and pulled off with POST. All works fine no problems, However if the user hits the back button the jump is made again and
3
1561
by: jodleren | last post by:
Hi I am wondering what goes wrong... I have session_cache_expire(30); session_start(); How can I check whether the _last_ session (from e.g. yesterday) has ended?
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.