Solved

php convert cookie to session

Posted on 2006-11-25
5
424 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:jk2001
jk2001 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
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.

911 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

19 Experts available now in Live!

Get 1:1 Help Now