Solved

php convert cookie to session

Posted on 2006-11-25
5
415 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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:jk2001
jk2001 earned 100 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now