php cookie secure javascript

user308553

I am using html and javascript for client, php for server. Each user, after signing up, will have a userID generated by server, and stored in database. In server I am using setcookie so that user don't have to log in every time he goes to another page.

  1. My first question is, should I just store the userID in the user's cookie for validation? How secure is that?

  2. My second question is how do I check for cookie every time a user open a page. Do I make a 'invisible' ajax call (sending its cookie by using getCookie("userID") in javascript) to server every time user open a page?

Ever since I finished school, I never know if I am doing things the right way, or if my codes are crap. How do you guys determine if your code is the 'right' way to do it, or is it just base purely on experience?

Marius Balčytis
  1. No, it would not be secure at all - cookies can be set and modified by the user.
  2. If you're using PHP (I think you are as there's a tag "PHP"), you should use SESSIONS.

Check the documentation: http://www.php.net/manual/en/book.session.php

Quick example:

<?php
session_start();
var_dump($_SESSION['user_id']);
$_SESSION['user_id'] = 123;

On first request it would print something like null, on other request - 123. It works by generating random value and setting to cookie, that is not easy to guess, then stores all session data to files or other storage by that generated key.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHP session.cookie_secure : true로 설정하면 세션 비활성화

분류에서Dev

self.get_secure_cookie returns None, but cookie exists

분류에서Dev

Secure folder structure for php site

분류에서Dev

PHP get cookie value

분류에서Dev

Javascript Cookie Undefined

분류에서Dev

PHP: Secure a Rest Service with a Token mixed with Timestamp

분류에서Dev

PHP: Can't Set Cookie

분류에서Dev

Javascript document.cookie if 문

분류에서Dev

How to use a test tornado server handler that authenticates a user via a secure cookie

분류에서Dev

간헐적으로 값을 반환하는 Tornado get_secure_cookie ()

분류에서Dev

龙卷风get_secure_cookie()间歇性返回值

분류에서Dev

php Cookie not working, won't get set

분류에서Dev

Delete cookie (PHP, JS) works in Firefox, Safari, Opera but not Chrome

분류에서Dev

PHP sessions in redis ignores session.cookie_domain configuration

분류에서Dev

Disable google ads after a visitor made a purchase, using cookie or javascript

분류에서Dev

I can only make a cookie have one value in javascript

분류에서Dev

PHP Fat-Free-Framework plus Node.js and Secure Websockets

분류에서Dev

Php output breaks the Javascript

분류에서Dev

Javascript passing a variable to PHP

분류에서Dev

Ajax PHP/Javascript array

분류에서Dev

JavaScript와 PHP

분류에서Dev

javascript php post undefined

분류에서Dev

PHP Variable in JavaScript Tag

분류에서Dev

Javascript in php not working?

분류에서Dev

Php and Javascript slider issue

분류에서Dev

Equating Javascript and PHP Variables

분류에서Dev

php in a javascript function not working

분류에서Dev

Create javascript alert in PHP

분류에서Dev

Loadmore Script Php / Javascript?