0

currently I'm doing code for login system. Login system will have remember me function(it will remember You for next 30 days). If user select yes for remember me function, server will create cookie and start session. If user desn't select yes for remember function, server will start only session user. Now I came to part when logged user trys to open page for login(prijava.php file). I put this code into jQuery file

$.ajax({
    type: "POST",
    url: "_hsync_scripts/_hsync_prijavljen.php",
    success: function(response)
    {
        if(Number(response) == 1) window.location.href = "index.php";           
    }       
}); 

It will run on every page load. File _hsync_prijavljen.php contains next code

<?php

if(isset($_COOKIE['_hsync_prijavljen'])) echo '1';
else if(isset($_SESSION['_hsync_prijavljen'])) echo '1'; // MAYBE PROBLEM IS HERE?
else echo '0';
?>

And login code

session_start();
if($_hsync_zapamti == 1) setcookie("_hsync_prijavljen", $_hsync_id, time() + 2592000, "/");
$_SESSION['_hsync_prijavljen'] = $_hsync_id; // MAYBE PROBLEM IS HERE?
echo '0';

If logged user trys to open login page, he will be redirect to index.php - that part Works. Part that doesn't work is next - if I log in without remember me function, and after that I type(in URL bar) link to login page, server won't redirect me to index.php. Why? This is frist time I worked with PHP sessions, maybe I don't understand the point of PHP sessions? I didn't closed tab, so seesion still must be active? Or not? Thanks!

Pararera
  • 363
  • 3
  • 15
  • 1
    `_hsync_prijavljen.php` should also have `session_start()` at the top. – Maximus2012 May 03 '16 at 21:00
  • I added `session_start()` but Know it always says I'm logged in. I tried with reopening tab, but nothing. – Pararera May 03 '16 at 21:04
  • 1
    Try adding `var_dump($_SESSION); exit;` to `_hsync_prijavljen.php` and see what you get. – Maximus2012 May 03 '16 at 21:07
  • 2
    I think you are confusing a `$_SESSION` cookie (a cookie that is stored on the server with only a unique identifier stored on the users computer that tells your server where to look for the cookie data) and a cookie stored in the browser that only lasts for the lifetime of the browser session. – Chris May 03 '16 at 21:08
  • 1
    `$_SESSION` is not a cookie. It is a PHP array stored on the server. – Sven May 03 '16 at 21:11
  • Guys, I Know that. But how seesion will stop? I've read on W3 that seesion will work until user close tab in browser. I closed browser and session still work. – Pararera May 03 '16 at 21:45
  • @Maximus2012 `var_dump` says http://i.imgur.com/7gb9ftb.png – Pararera May 03 '16 at 22:49
  • Does somebody knows? – Pararera May 04 '16 at 16:03

1 Answers1

2

Your code seems fine but _hsync_prijavljen.php also needs session_start();, i.e.:

<?php
session_start();
if(isset($_COOKIE['_hsync_prijavljen'])) echo '1';
else if(isset($_SESSION['_hsync_prijavljen'])) echo '1'; // MAYBE PROBLEM IS HERE?
else echo '0';
?>

Make sure you understand the difference between $_SESSION (info stored on webserver) and $_COOKIE (stored on user device).

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I Know, but I closed tab in browser, should then session on web server stop or not? When session will stop? – Pararera May 03 '16 at 21:32
  • I don't want to extent time of session. I don't Know why session won't stop when user close browser(or tab). – Pararera May 03 '16 at 22:56
  • 1
    Thanks! I did mistake. If session _hsync_prijavljen' doesn't exist, PHP will print error and echo. I fixed it. – Pararera May 05 '16 at 11:04