1

Below is a simple registration form, using which a user after registering will be redirected to main content, means session id will be created here itself. Now I want to set session that does not expire on browser exit but I don't know how to do it exactly.

 <?php
 if(isset($_POST['reg'])){

$fn=mysqli_real_escape_string(@$_POST['fname']);
$ln=mysqli_real_escape_string(@$_POST['lname']);
$un=mysqli_real_escape_string(@$_POST['username']);
$pw=mysqli_real_escape_string(@$_POST['password']);
$stmt = "INSERT INTO userss (userid,username,first_name,last_name,password) VALUES ('','{$un}','{$fn}','{$ln}','{$pw}')";

if ($mysqli->query($stmt) === TRUE) {
echo "<script type= 'text/javascript'>alert('Welcome');</script>";
 session_start();
 $_SESSION['user'] = $userid;
 header("Location: main.php");
} 
else {
echo "<script type= 'text/javascript'>alert('Error: " . $stmt . "<br>" . $mysqli->error."');</script>";
}
?>

Should I add setcookie after session_start(); like I have done below or their is some other better way.

session_start();
setcookie(session_name(),time()+10 * 365 * 24 * 60 * 60);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

0 Answers0