PHP - Session_regenerate_id
I am trying to implement session_regenerate_id but am a little uncertain about the code from my book.
// Check for match. if (mysqli_num_rows($r) == 1){ // User found. // Fetch User info. $row = mysqli_fetch_array($r, MYSQLI_NUM); // Note A $_SESSION['user_id'] = $row[0]; // Assign UserID to Session. $_SESSION['username'] = $row[1]; // Assign Username to Session. /* ORIGINAL CODE if ($row[2] == 'admin'){ $_SESSION['user_admin'] = true; // Assign to Session. } if ($row[3] == 1){ $_SESSION['user_not_expired'] = true; // Assign to Session. } */ // NEW CODE if ($row[2] == 'admin'){ // Call before storing any session data, because passing "true" as // the 1st argument causes any existing session data to be destroyed. session_regenerate_id(true); $_SESSION['user_admin'] = true; // Assign to Session. // Will this over-write any data from above (see Note A)?? $_SESSION['user_id'] = $row[0]; // Assign UserID to Session. $_SESSION['username'] = $row[1]; // Assign Username to Session. } if ($row[3] == 1){ $_SESSION['user_not_expired'] = true; // Assign to Session. } } else { My question is... // Will this over-write any data from above (see Note A)?? It is nested in the code above along with "Note A". Thanks, TomTees Similar TutorialsWhy am i Getting this? Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in ..... Code: [Select] <?php if(isset ($_POST['email'])){ //Start session session_start(); require_once "scripts/mysqlconnect.php"; $remember = $_POST['remember']; // Added for the remember me feature // Make the posted variable SQL safe $email = eregi_replace("`", "", mysql_real_escape_string(strip_tags($_POST['email']))); $password = md5(eregi_replace("`", "", mysql_real_escape_string(strip_tags($_POST['password'])))); // Create query. !! You need to rename your 'username' column in your database to 'email' !! $qry = "SELECT * FROM members WHERE email='$email' AND password='$password' AND email_activated='1'"; // Run query $result=mysql_query($qry); //Check whether the query was successful or not if($result) { // If one row was returned (if there was a match) if(mysql_num_rows($result) == 1) { // Login Successful // Get a new session ID session_regenerate_id(); // Get the row as an array $member = mysql_fetch_assoc($result); // Create session variables $_SESSION['LOGINID'] = $member['loginid']; $_SESSION['EMAIL'] = $member['email']; $_SESSION['USERNAME'] = $member['username']; // Stop writing to the session session_write_close(); // Create a variable for the member ID, you can't include $member['id'] in the SQL statement $id = $member['loginid']; // Update the table with the current time mysql_query("UPDATE members SET last_log_date=NOW() WHERE loginid='$id'"); // Remember Me Section Addition... if member has chosen to be remembered in the system if($remember == "yes") { setcookie("idCookie", $id, time()+60*24*60*60, "/"); setcookie("usernameCookie", $username, time()+60*24*60*60, "/"); setcookie("emailCookie", $email, time()+60*24*60*60, "/"); setcookie("passwordCookie", $password, time()+60*24*60*60, "/"); } // Redirect to the members only page //header("location: ".$_SERVER['PHP_SELF'].""); /* Quick self-redirect to avoid resending data on refresh */ echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">"; return; exit(); } } else { die("Query failed"); } } ?> <style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; color: #0F0; } #apDiv1 { position:relative; width:241px; height:0px; z-index:1; left: -270px; top: 0px; } --> </style><div align="center"> <div id="apDiv1"> <form action="" method="post"> <p>Email <input type="text" name="email" id="email" size="15" /> </p> <p>Password <input type="password" name="password" id="password" size="15"/> </p> <p> Remember <input type="checkbox" name="Remember" id="Remember" /><input name="Submit" type="submit" value="Login"/> </p> </form> </div> <img src="/images/header.jpg" width="950" height="100" /> </div> |