Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:31+00:00 2026-06-05T22:28:31+00:00

So this is my first time using cookies and i’m having some trouble setting

  • 0

So this is my first time using cookies and i’m having some trouble setting them. I think i’m doing everything correctly (but you know how programming can be). anyways I am creating a login system that will through a jquery.ajax call in validateLogin.js, to one php script (autologin.php), check if a user and their password are in a database by way of a function in another script (login.php), in login.php if the user exists and the password is the same the cookie is set and some json data is returned to autologin.php which then returns some more json data to the original jquery.ajax methods success function. the success function will then redirect to userarea.html, where a js function will automatically load the user data so it is available for use. this is done with another ajax call in loaduserdata.js which calls to loaduserdata.php which checks isset($_COOKIE['user'], and then returns some json data depending on what happens. however the isset($_COOKIE['user'] fails and i’m not getting any errors in the console or in any of the logs (php, mysql, apache). So i’m really stumped. here is my code:

validateLogin.js

function validateLogin(){
  $(document).ready(function(){
    $("#loginform").submit(function(){
      $.ajax({
        type: "POST",
        url: "./php/autologin.php",
        data: {
          'login': $("#login").val(), 
          'password': $("#password").val()
        },
        dataType: "json",
        success:function(data){
          if(data.status == "success"){
            alert(data.message);
            window.location = "./userarea.html";
          } else if (data.status == "error"){
            alert(data.message);
          }
        },
        error:function(thrownError){
          console.log(thrownError);
        }
      }); 
      return false;
    });

  });
}

autologin.php

<?php

include './login.php';
$login = $_POST['login'];
$password = $_POST['password'];
$loginattempt = login($login, $password);
$loginattemptdata = json_decode($loginattempt);
if ($loginattemptdata->{'status'} === "success") {
  echo json_encode(array('status' => "success", 'user' => $loginattemptdata->{"user"}, 'message' => "Login Successful!"));
  die();
} else {
  echo json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => $loginattemptdata->{"message"}));
  die();
}
?>

login.php

<?php
include './connect_to_mysql.php';

function login($log, $pass) {
  $link = connect_to_mysql();
  $linkdata = json_decode($link);
  if ($linkdata->{'status'} === "success") {
    $sqlquery = mysql_query("SELECT * FROM userdata WHERE login='$log' AND password='$pass'") or die(mysql_error());
    if (mysql_num_rows($sqlquery) == 1) {
      setcookie("user", $log, 86400, '/', 'localhost');
      return json_encode(array('status' => "success", 'message' => "Login Successful.", 'user' => $log));
      die();
    } else {
      return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => mysql_error()));
      die();
    }
  } else {
    return json_encode(array('status' => "error", 'error' => "connectionerror", 'message' => $linkdata->{'message'}));
    die();
  }
}

?>

loaduserdata.js

$(document).ready(function(){
  $.ajax({
    type:"POST",
    url:"./php/loaduserdata.php",
    success:function(data){
      if(data.status === "success"){
        alert(data.status);
        alert(data.user);
      } else if (data.status === "error"){
        alert(data.status);
        alert(data.message);
        window.location = "./index.html";
      }
    },
    error:function(thrownError){
      console.log(thrownError);
    }
  });
});

loaduserdata.php

<?php
if (isset($_COOKIE['user'])) {
  $user = $_COOKIE['user'];
  echo json_encode(array('status' => "success", 'message' => $user));
  die();
} else {
  echo json_encode(array('status' => "error", 'message' => "Please login before continuing."));
  die();
}
?>

from what i’ve read you need to either refresh or redirect after setting a cookie for it to be available to a new script, which i believe is what i am doing. any help or direction would be greatly appreciated. thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:28:33+00:00Added an answer on June 5, 2026 at 10:28 pm

    Relying on a client side cookie for login status is a bad idea. Imagine what happens if the user crafts his own cookie with a random (valid) username? He will have gained access to your system without need for a password.

    Have you considered switching to a $_SESSION-based authentication procedure?

    The concept is that every visitor is assigned a unique ID (the session ID) which is stored as a cookie client side. When the visitor loads a page, the sessid cookie is passed along and php loads it from a session storage (most likely a file in /tmp/ if you haven’t configured sessions yet). This file is reflected with the $_SESSION variable, in which you can save data that is only accessible to that corresponding session ID. Thus, usernames or login status is handled server-side and never client side.

    <?php
    # login.php
    include './connect_to_mysql.php';
    
    function login($log, $pass) {
      $link = connect_to_mysql();
      $linkdata = json_decode($link);
      if ($linkdata->{'status'} === "success") {
        $sqlquery = mysql_query("SELECT * FROM userdata WHERE login='$log' AND password='$pass'") or die(mysql_error());
        if (mysql_num_rows($sqlquery) == 1) {
          session_start();
          $_SESSION['user'] = $log;
          return json_encode(array('status' => "success", 'message' => "Login Successful.", 'user' => $log));
        } else {
          return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => mysql_error()));
        }
      } else {
        return json_encode(array('status' => "error", 'error' => "connectionerror", 'message' => $linkdata->{'message'}));
      }
    }
    ?>
    

    Changes: (1) drop the die() after return. They are never executed. (2) save to session via session_start() and $_SESSION[‘user’] = $log; instead of using your own cookies.

    <?php
    # loaduserdata.php
    session_start();
    if (isset($_SESSION['user'])) {
      $user = $_SESSION['user'];
      echo json_encode(array('status' => "success", 'message' => $user));
      die();
    } else {
      echo json_encode(array('status' => "error", 'message' => "Please login before continuing."));
      die();
    }
    ?>
    

    Changes: use session_start() and $_SESSION[‘user’] rather than relying on client side cookies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new to android and this my first time using AsyncTask. I made
This is my first time using XML documents. What I'm trying to do is
This is my first time using this site and I am quite new to
This is my first time using lightbox which uses jquery framework. But when I
This is my first time using an external library, and I'm a bit nervous
This is my first time using jQuery and I am pleased to get my
This is my first time using Google Analytics Ecommerce Tracking to get data for
this is my first time using StAX for parsing XML documents (still in the
Sorry, this is my first time using this forum. Apparently people can edit my
I am using cookies. When you first time open the page i set the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.