473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

PHP Session question...

Hey all.
Trying to get sessions to work on my app. The issue is this. When the
main page loads I check for a session variable uname to see if the
user is logged in and display a "hello, username" message, or
otherwise use "guest." Pretty straightforward. Then I have a login
form that uses AJAX calls to log the user in. If the login info is
correct, I set $_SESSION['uname'] to the username. However, if I then
refresh the main page, the session info is gone. Here's a glimpse at
what's going down....

From index.php:
<?php
session_start();

?>
....
<span>Hello, <?php if(isset($_SESSION['uname'])){
echo $_SESSION['uname'];
}
else {
echo 'Guest';
}?></span>

From login.php:
<?php

$dbh=mysql_connect ...

...
//do this on login SUCCESS:
echo 'Login Successful';
$_SESSION['uname'] = $_POST['un'];
};
?>
I can't use session_start() in login.php; it gives me an error saying
http headers have already been sent.

Any suggestions?
Apr 11 '08 #1
2 1396
dysfunct wrote:
Hey all.
Trying to get sessions to work on my app. The issue is this. When the
main page loads I check for a session variable uname to see if the
user is logged in and display a "hello, username" message, or
otherwise use "guest." Pretty straightforward. Then I have a login
form that uses AJAX calls to log the user in. If the login info is
correct, I set $_SESSION['uname'] to the username. However, if I then
refresh the main page, the session info is gone. Here's a glimpse at
what's going down....

From index.php:
<?php
session_start();

?>
...
<span>Hello, <?php if(isset($_SESSION['uname'])){
echo $_SESSION['uname'];
}
else {
echo 'Guest';
}?></span>

From login.php:
<?php

$dbh=mysql_connect ...

...
//do this on login SUCCESS:
echo 'Login Successful';
$_SESSION['uname'] = $_POST['un'];
};
?>
I can't use session_start() in login.php; it gives me an error saying
http headers have already been sent.

Any suggestions?
You need session_start() at the beginning of EVERY page which uses sessions.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Apr 12 '08 #2
On Sat, 12 Apr 2008 03:48:21 +0200, Jerry Stuckle
<js*******@attglobal.netwrote:
> I can't use session_start() in login.php; it gives me an error saying
http headers have already been sent.
Any suggestions?
You need session_start() at the beginning of EVERY page which uses
sessions.
Indeed, and you should be able to start a session on every request where
it's needed BEFORE generating any output. The error message will tell you
where the output started, get your session_start() before that.
--
Rik Wasmus
Apr 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Xizor | last post by:
Let's say I run a server. I have two people using the server. Bill and Joe. Bill is at address.com/bill and Joe is at address.com/joe. Let's say Joe and Bill are both using PHP with sessions on...
1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
3
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available...
1
by: Ernest Forman | last post by:
How can I view the defined Session Variables in the Dotnet environment? I found the session object and it shows me the count of the number of items, but I don't see how to see the names and values...
3
by: Jeff Smythe | last post by:
I simply want to execute some code once when a new session of my ASP.NET application is started (I'm not using session state for anything else - just writing some data to a database). I thought...
4
by: DavidS | last post by:
First: There are several ways to confuse one regarding session timeout. (1) web.config - <sessionState timeout="20"> (2) IIS Manager | Internet Information Services | ServerNode | Default Web Site...
10
by: tshad | last post by:
I have been using the default session state (InProc) and have found that I have been loosing my information after a period of time (normally 20 minutes). Is there anyway to find out how much...
18
by: Rippo | last post by:
Hi I am using role base forms authentication in asp.net and have come across a problem that I would like advice on. On a successful login a session variable is set to identify a user. This is...
5
by: George | last post by:
Hi, Is it possible to delay session creation in ASP.NET I want the user to browse my site without any session created (no cookies set). My pages will know that is Session = null then Session has...
2
by: Kevin Frey | last post by:
Hello, I've been reading that ASP.NET serialises (ie. processes one at a time) HTTP requests if two simultaneous requests need to access the same session state. It also makes note that ASP.NET...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.