Y
Y
Yevhenii K2015-10-30 13:44:23
PHP
Yevhenii K, 2015-10-30 13:44:23

Cookies are not saved, what could be the problem?

Good afternoon! I have one problem with cookies, I can not figure it out.
I have a method which checks the user's data during authorization, after which it creates cookies. I enter data on the main page, they are sent to a script file in which

require_once ("../classes/user.php");

  $new_user = new User($_POST);
  $new_user->checkUser();
  unset($_POST);

at the moment the script is running, cookies are created, but after returning to the page. Disappear.
method
public function checkUser() {                                        //authentication
    $ques = "SELECT * FROM `users` WHERE `users`.`login` = '$this->login'";
    $get_us = DB::obj()->connect()->query($ques);
    $r = $get_us->fetchAll(PDO::FETCH_ASSOC);

    if ($r[0]['login'] === $this->login && $r[0]['password'] === $this->password) 
    {
      setcookie('user_id',$r[0]['id'],time()+3600);
      setcookie('login',$r[0]['login'],time()+3600);
      header("Refresh:1; http://site.loc/index.php");
    }	
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2015-10-30
@AFI19

Try passing the 4th parameter to the setcookie method with the value '/'
The path to the directory on the server from which cookies will be available. If set to '/', cookies will be available in the entire domain. If set to '/foo/', cookies will only be available from the /foo/ directory and all its subdirectories (for example, /foo/bar/) of the domain. The default value is the current directory where the cookie is set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question