Solved

php convert cookie to session

Posted on 2006-11-25
5
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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.

734 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