Hi folks would really appreciate your help in answering this rather involved question. Please don't dismiss me on length, I promise it's an easy to comprehend read!
When and how should you obfuscate (if at all) $_SESSION and $_COOKIE variables in context to login.php and login_validation.php for the following membership based website?
This membership based website is written in php and uses mysql as its relational database.
login.php
- <?php
- //Connect to database
- $email = $_POST['email']; //from a text field
- $password = $_POST['password']; //from a password field
- $password= md5($password);
- $stayLoggedIn= $_POST['stayLoggedIn']; //from a checkbox, value="yes" for checked, value="no" for unchecked
-
- $sql = "SELECT id
- FROM users
- WHERE email='$email' AND password='$password'";
-
- $query=mysql_query($sql);
-
- if(mysql_num_rows($query)==1){
- $row = mysql_fetch_array($query);
- $id = $row["id"];
- $encodedID_session= base64_encode("iofj4983rn9dh83$id");
- $_SESSION['id'] =$encodedID_session;
-
- if($stayLoggedIn== 'yes'){
- $encodedID_cookie=base64_encode("dj02359t5ng842$id");
- setcookie("idCookie", $encodedID_cookie, time()+60*60*24*7, "/");
- setcookie("passwordCookie", $password, time()+60*60*24*7, "/");
- }
- }
- ?>
He has a file called login_validation.php which goes at the top of every single page of the website (except login.php).
login_validation.php
first it decodes the session and cookie variables if they are set (example for decoding session variable below)
- $decodedID = base64_decode($_SESSION['id']);
- $array = explode("iofj4983rn9dh83", $decodedID);
- $id = $array[1];
Then it does only one of the following:
- if the session AND cookie are not set it displays the header "You are not logged in"
- if the session is set, it queries mysql to find the name of the person in the same row as his or her id and displays the personalized header "You are logged in, welcome Bonzo!"
- if the session is not set but the cookies are set (ex. user selected stayLoggedIn, shut down his computer, and opened up a fresh browser) it queries mysql to find the name of the person in the same row as his or her id and displays the personalized header "You are logged in, welcome Bonzo!"
The developer has done a few things which I left me a little confused like obfuscating a session variable, or obfuscating two (instead of one) cookie. Also his method of obfuscation using base64 seems vulnerable to threat if someone got a hold of his nonsense strings he appends.
When and how should you obfuscate (if at all) $_SESSION and $_COOKIE variables in context to login.php and login_validation.php for this membership based website?
Thank you, thank you!
EDIT: Just to clarify I did not pay anyone for this code, instead I'm trying to learn php and got this code from a php tutorial which looked like it could be improved.
EDIT 2: The password is now hashed before validation