/    Sign up×
Community /Pin to ProfileBookmark

Secure Cookies

Hi Guys,
I was wondering how can I make my cookies secure so that they can not be played around with?! For example I need to encrypt the user_id of the user when sending it.

Basically what is the right approach to using cookies but not becoming vulnerable to hacks.

Thanks.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@svidgenOct 14.2010 — Any data the user sends you is susceptible to tampering. You can reduce the risk by making critical data difficult to spoof. So, to your question: Don't store sensitive data in cookies. Use PHP's built-in session handling. It's highly unlikely that a malicious user will be able to guess the session ID of another user. And you can then store all sensitive data in the session. (server-side)

http://www.svidgen.com/search?cof=FORID:9&ie=ISO-8859-1&q=php+sessions&sa=Search&cref=http://www.svidgen.com/cse.xml%3Fv%3D318&siteurl=www.svidgen.com/
Copy linkTweet thisAlerts:
@XeroSiSauthorOct 17.2010 — Thanks.

But the problem is that Sessions end after user closes the browser. Some users would not like to re-login every time they come to the site.

How should I best approach this? I am guessing I save 1 cookie on their computer and then retrieve their information and place it as sessions. but what is that 1 thing that should be saved to keep things secure?
Copy linkTweet thisAlerts:
@NogDogOct 17.2010 — Thanks.

But the problem is that Sessions end after user closes the browser. Some users would not like to re-login every time they come to the site.

How should I best approach this? I am guessing I save 1 cookie on their computer and then retrieve their information and place it as sessions. but what is that 1 thing that should be saved to keep things secure?[/QUOTE]


Sessions only end when the browser is closed [b]if[/b] that is the setting you are using for session.cookie_lifetime. You also need to set the [url=http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime]session.gc_maxlifetime[/url] to how long you want the session data to be stored on the server. (If on a shared host or if other applications with different session settings are running on your host, make sure to have your application use its own session.save_path so that the data does not get "garbage collected" by other applications using the same directory.)

You can also control these settings at the script level via [url=http://www.php.net/manual/en/function.session-set-cookie-params.php]session_set_cookie_params[/url]() and [url=http://php.net/ini_set]ini_set[/url](), but then you must be sure to do it in each script before the call to session_start() (perhaps in an include file?).
Copy linkTweet thisAlerts:
@XeroSiSauthorOct 18.2010 — wow, this is getting quite complicated for me, can you refer me to a site which i can get more information on this?

Thanks.
Copy linkTweet thisAlerts:
@NogDogOct 18.2010 — wow, this is getting quite complicated for me, can you refer me to a site which i can get more information on this?

Thanks.[/QUOTE]


Off-hand I don't know of anything other than the manual pages I linked above.

A session include file might be something like:
[code=php]
<?php
$session_lifetime = 60 * 60 * 24 * 30; // 30 days
$session_domain = ".example.com"; // note leading dot
// make sure the specified directory is writable by the web server:
$session_path = $_SERVER['DOCUMENT_ROOT'] . '/../session_data';

if(!headers_sent()) {
session_set_cookie_params($session_lifetime, '/', $session_domain);
ini_set('session.save_path', $session_path);
ini_set('session.gc_maxlifetime', $session_lifetime);
session_start(); // session will be started by this include file.
}
else {
error_log("Headers already sent error, could not start session");
die("There was a problem initializing your session");
// or whatever you want to do in this case
}
[/code]

Then just include that file at the very beginning of each script you want to be session-controlled.
Copy linkTweet thisAlerts:
@XeroSiSauthorOct 19.2010 — Thanks a lot. Will do.
×

Success!

Help @XeroSiS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...