473,386 Members | 1,804 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,386 software developers and data experts.

Problems with PHP and sessions

PHP
I have a login page, that have this code:

It is called from a page with a form and 3 inputs, email, password and
autologin.

<?PHP

mysql_connect("localhost", "user", "psw");
mysql_select_db("database");

$email = $_POST["email"];
$psw = $_POST["psw"];
$autologin = $_POST["autologin"];

if($autologin == "on")
session_set_cookie_params(60*60*24*365, "/");
else
session_set_cookie_params(0, "/");

session_start();
$_SESSION["email"] = $email;
$_SESSION["psw"] = $psw;

session_write_close();

mysql_query("UPDATE blaa....") or die(mysql_error());

header("Location: /?page=user_login");

exit();
?>
My index.php looks like this:

<?PHP

if(!empty($_COOKIE["PHPSESSID"]))
{
session_start();
$email = $_SESSION["email"];
$psw = $_SESSION["psw"];
}

.... blaa... ?>

My problem is, that if set the "autologin = on" (that means the cookie
must have a 365 days expire), my cookie is set perfectly on the login
page but as soon as it is redirected to the index page, it overwrite the
cookie with an expire whenever the browser closes!?
Why?

I have seen on the PHP manual page, that there can be a lot of problems
if you use a "header", but how do I fix this problem (if possible)? I
Jul 17 '05 #1
3 1775
SEssion cookes only exist for the duration of the session, and are normally
deleted by the browser when u close the window.

Your server will also delete the session info from ur server after 20
minutes (or whatever the default is set to).

For auto login, save the username and password in a cookie that is saved in
the users computer, and then check for the cookie existance.

(I probably wouldnt store the password, store some random key).

- Alistair

"PHP" <sd****@REM.hotmail.com> wrote in message
news:42***********************@dreader1.cybercity. dk...
I have a login page, that have this code:

It is called from a page with a form and 3 inputs, email, password and
autologin.

<?PHP

mysql_connect("localhost", "user", "psw");
mysql_select_db("database");

$email = $_POST["email"];
$psw = $_POST["psw"];
$autologin = $_POST["autologin"];

if($autologin == "on")
session_set_cookie_params(60*60*24*365, "/");
else
session_set_cookie_params(0, "/");

session_start();
$_SESSION["email"] = $email;
$_SESSION["psw"] = $psw;

session_write_close();

mysql_query("UPDATE blaa....") or die(mysql_error());

header("Location: /?page=user_login");

exit();
?>
My index.php looks like this:

<?PHP

if(!empty($_COOKIE["PHPSESSID"]))
{
session_start();
$email = $_SESSION["email"];
$psw = $_SESSION["psw"];
}

... blaa... ?>

My problem is, that if set the "autologin = on" (that means the cookie
must have a 365 days expire), my cookie is set perfectly on the login page
but as soon as it is redirected to the index page, it overwrite the cookie
with an expire whenever the browser closes!?
Why?

I have seen on the PHP manual page, that there can be a lot of problems if
you use a "header", but how do I fix this problem (if possible)? I

Jul 17 '05 #2
PHP
> My index.php looks like this:

<?PHP

if(!empty($_COOKIE["PHPSESSID"]))
{
session_start();
$email = $_SESSION["email"];
$psw = $_SESSION["psw"];
}

... blaa... ?>

I have found my problem - I should not have a session_start() in the
index.php also as this overwrites the existing one. That took me some
time to understand! I thought I should have session_start on all pages.
Jul 17 '05 #3
PHP wrote:
I thought I should have session_start on all pages.


AFAIK, you do! Session_start() loads the session variables in for the
current session.

Cheers,
Nicholas Sherlock
Jul 17 '05 #4

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

Similar topics

2
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...
2
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...
0
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...
7
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...
3
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...
4
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...
3
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....
20
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...
5
by: dipperdan | last post by:
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...
3
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.