PHP Session Lost -
i know there lot of questions out there dont give info or use other methods authenticate. lets begin:
i have login page , main page. login saving session in $_session in every page make session_start() , check if user loged looking $_session.
when login redirects me main page. when try login.php detects correctly im in , redirects main.php. when refresh main.php detects correctly session.
then, want make change in db use jquery function $.post send post data action.php , staff. action.php echo "true" $.post handler can detect change has been made correctly , redirect. action.php echo "false" $.post handler alert error ocured.
when make change db, gets done , echo true main.php refresh showing change instead of refresh come login.php losing session data.
here code:
start_session
function sec_session_start() { $session_name = 'sec_session_id'; // set custom session name $secure = false; // set true if using https. $httponly = true; // stops javascript being able access session id. ini_set('session.use_only_cookies', 1); // forces sessions use cookies. $cookieparams = session_get_cookie_params(); // gets current cookies params. //session_set_cookie_params($cookieparams["lifetime"], $cookieparams["path"], $cookieparams["domain"], $secure, $httponly); session_set_cookie_params(7200, "/", $cookieparams["domain"], $secure, $httponly); session_name($session_name); // sets session name 1 set above. session_start(); // start php session session_regenerate_id(true); // regenerated session, delete old one. }
top of main.php , login.php , action.php
include('../db_connect.php'); include('../functions.php'); sec_session_start(); // our custom secure way of starting php session. if(!login_check($mysqli)){ header('location: ../'); }
javascript function post data action.php
function createteam(){ var serializeddata = $('#new_team_form').serialize(); $.post('action.php', serializeddata , function (resp) { if(resp == "false"){ $('#exists').html("el grupo que intentas crear ya existe o ha habido un error en la peticiĆ³n."); }else{ window.location ="./"; } }); return false; //needs return false cause submit button of form }
action.php - echoed return $.post()
<?php include '../db_connect.php'; include '../functions.php'; sec_session_start(); // our custom secure way of starting php session. $team = $_post['team']; $_session['user_id'] = $user_id; if ($stmt = $mysqli->prepare("select name teams name = ? limit 1")) { $stmt->bind_param('s', $team); // bind "$user_id" parameter. $stmt->execute(); // execute prepared query. $stmt->store_result(); if($stmt->num_rows == 1) { echo "false1".$team; } else { //creamos el grupo en la base de datos y ponemos al usuario en dicho grupo if ($stmt = $mysqli->prepare("insert teams (id, name) values (default, '$team')")) { $stmt->execute(); // execute prepared query. if($stmt = $mysqli->prepare("update members set team = '".$team."' members.id ='".$user_id."' limit 1 ")) { $stmt->execute(); // execute prepared query. echo "true"; }else{ echo "false2"; } }else{ echo "false3"; } } } else { echo "false4"; } ?>
what think & supposed answer problem
as trying mention main.php there no session transfer action.php except things running correctly ..
as reviewed code & know session work
//the session retrieve values on form of :
$_session['user_id'] = $user_id;
//and shifted page session retrieved :
$user_id = $_session['user_id'];
so think in action.php session not writes in retrieval values of syntax form try write down there
$user_id = $_session['user_id'];
Comments
Post a Comment