Sidath Sidath - 4 months ago 13
PHP Question

session_set_cookie_param function is not working

I have already created a php application for authenticate that is for our club members so I need to set member name as a $_SESSION['name'] variable and I need to keep that for 1 hour. For do this job I have to use session_set_cookie_param() php function but that is not working with my values. I tired every methods but it's not working all. In this application, I'm use connector.php file and login index page. $user->login(), $user->signup() is my own functions that are return 1 if it is success. If authenticate is success, it's overwrite new session value as member name and redirect home page index.php file

//set session for 1 hour
session_set_cookie_pram('/','60');
session_start();
session_regenerate_id();

// Make db connection
define('USER_NAME','root');
try{
$conn = new PDO("mysql:host=localhost;dbname=ajax_msg",USER_NAME,'');
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
}
//create a User class for handle user actions
Class User{
private $db;
private $pass;
public $name;

//Loading data when load user Class
public function __construct($conn){
$this->db = $conn;
$this->pass = $_POST['pass'];
$this->name = $_POST['name'];
}

public function redirect($url){
//echo "<script>setTimeout(function () {document.getElementById('status').innerHTML(window.location = '{$url}');},2000);</script>";
echo "<script>window.location = '{$url}';</script>";
}

public function chkfield($data){
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

public function login(){
try{
$username = $this->chkfield($this->name);
$userpass = $this->chkfield($this->pass);
$setpass = sha1($userpass);
$sql = "SELECT * FROM ajax_user_info WHERE ajax_username=:username AND ajax_userpass=:setpass";
$exe = $this->db->prepare($sql);
$exe->bindValue(":username",$username,PDO::PARAM_STR);
$exe->bindValue(":setpass",$setpass,PDO::PARAM_STR);
$exe->execute();
if($exe->rowCount() == 1){
$exe->setFetchMode(PDO::FETCH_ASSOC);
$i=0;
while($i <= $t=$exe->fetch()){
$_SESSION['ajax_user'] = $t['ajax_username'];
$i++;
}
return true;
}else{
return false;
}
}catch(PDOException $e){
return false;
echo $e->getMessage();
}

}


Here is my login php file

// This is authentication php file. this is parameters of this file
// Username = name , Userpassword = pass
if(empty($_POST['name'])===false && empty($_POST['pass'])===false){
require_once('connector.php');

if($user->login()){
?><script>$('#status').append("<div class ='status_ok'>Success! Welcome to SkyFace</div>");</script><?php
$user->redirect('../what');
}else{
?><script>$('#status').append("<div class ='status_err'>Oops! Username or password is wrong</div>");
setTimeout(function(){$('.status_err').fadeOut(300);$('.status_err').remove();},3000);</script><?php
}
}


Here is my homepage index file

include_once('dbconnector.php');
if(!isset($_SESSION['ajax_user'])){
header("location:../log");
}


Where i went wrong?

Answer

Try this example:

<?php 
session_set_cookie_params(30 * 60, "/");
session_start();
print_r(session_get_cookie_params());
?>

Output:

Array ( [lifetime] => 1800 [path] => / [domain] => [secure] => )

Explanation:

Sets the cookie to expire after 30 minutes and be available anywhere on the site.

Comments