knobby2k Posted September 8, 2011 Share Posted September 8, 2011 Hi guys, quick and simple question, should you cleanse the data that you recover from a session. i.e. i have my username in a session to ensure the user is logged in... so, on my page should i be cleansing the data with the various striptags, stripslashes, htmlspecialchars, etc... OR as long as I check the data matches what i expect to be entered at the time it is input by the user, will that data still be safe when i call the session? I suppose what I am asking is can a malicious user spoof a session, so I call $_SESSION['username'] and it turn out to be $_SESSION['lots of damaging code'] Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246737-cleanse-data-in-sessions/ Share on other sites More sharing options...
voip03 Posted September 8, 2011 Share Posted September 8, 2011 This article for you. http://stackoverflow.com/questions/328/php-session-security http://www.sitepoint.com/notes-on-php-session-security/ Quote Link to comment https://forums.phpfreaks.com/topic/246737-cleanse-data-in-sessions/#findComment-1267086 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 You should call htmlspecialchars() on anything you want to echo that you don't expect to contain HTML code, unless you KNOW that variable can't contain malicious data (integers, floats, etc). It's very hard for a user to change session data unless you allow them to.. for example $_SESSION['someVar'] = $_GET['something']; If you're on a shared host, and the host hasn't locked down the session files, it's possible for another user on the same machine to inject malicious code into a session. This is unavoidable from a programmers perspective, and the only way to protect from this RARE form of attack is to sanitize every session variable before use (htmlspecialchars for display, mysql_real_escape_string() for mysql). This is a little extreme though, as the attack can be hard to pull off. Any time any form of data is supplied by an outside source, you should sanitize it before use. Quote Link to comment https://forums.phpfreaks.com/topic/246737-cleanse-data-in-sessions/#findComment-1267089 Share on other sites More sharing options...
knobby2k Posted September 8, 2011 Author Share Posted September 8, 2011 cheers you lot!! Quote Link to comment https://forums.phpfreaks.com/topic/246737-cleanse-data-in-sessions/#findComment-1267092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.