Solved

php convert cookie to session

Posted on 2006-11-25
5
452 Views
Last Modified: 2012-05-05
I have a website that uses php cookies.
Is there an easy way to convert it to session variables?

What would I need to do?

Also, is there anything I should watch out for?
0
Comment
Question by:jackjohnson44
5 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 18012399
foreach($_COOKIE as $key=>$value) {
    $_SESSION[$key] = $value;
}

I don't think you need to watch out for anything, sessions are safer then cookies anyway...
0
 

Author Comment

by:jackjohnson44
ID: 18012538
do I have to put anything else in my script? start the session

Also will a session persist across a move from the http side to https side of the domain?
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 300 total points
ID: 18012703
> do I have to put anything else in my script? start the session
Use
session_start();
at the beginning of your php page to start the session

> Also will a session persist across a move from the http side to https side of the domain?
Yes, that shouldn't be a problem.
0
 
LVL 8

Assisted Solution

by:John Kawakami
John Kawakami earned 100 total points
ID: 18012881
Just to add on TeRReF's comment - sessions are implemented with cookies.  The session data is written to a file, and the name of that file is stored in a browser cookie named PHPSESSID.
0
 
LVL 1

Assisted Solution

by:bobbyo
bobbyo earned 100 total points
ID: 18044558
One thing to add. There is a setting in the php.ini file that will start sessions automatically

session.auto_start = 1

As for session value been stored in the cookie. This can also be enabled or disabled in the php.ini file

session.use_cookies = 1

If it is set to 0 the session variable normally PHPSESSID is maintained via the url. If you use normal href tags or forms the id should be maintained automatically in the url however if you use javascript redirects it will be lost.
0

Featured Post

Independent Software Vendors: 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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 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