S
S
Space2014-03-19 09:14:46
JavaScript
Space, 2014-03-19 09:14:46

How to pull out the right cookie?

Hello. There is a code, yesterday a man threw

var get_cookies = function(request, cookieName) {
    var cookies = {};
    request.headers && request.headers.cookie.split(';').forEach(function(cookie) {
    var parts = cookie.match(/(.*?)=(.*)$/)
    cookies[ parts[1].trim() ] = (parts[2] || '').trim();
    });
    return cookies[cookieName];
  };

Question: How to get the 'user' cookie from this code? I know, it's a noob question, but I'm asking for help, because this code is suitable for a server on node.js, while others are not, because it does not read document or anything else.

ps no jquery to offer :)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
O
Olga Stogova, 2014-03-19
@dessert

You can also connect plugins.jquery.com/cookie
After that, you can simply work with cookies:
// this is how you can set new cookies or overwrite the values ​​of existing ones:
// you can get the value of existing cookies like this:
// if the requested cookies do not exist , then this function will return null
// otherwise you can delete cookies
$.cookie('cookie_name', null);

Q
q1t, 2014-03-19
@q1t

googled a simple lib https://github.com/defunctzombie/node-cookie
you can not use it, but just look how it parses data from the header
. Express frameworks already have built-in work with cookies

S
Space, 2014-03-19
@ruslite

@icelaba where is your answer? where did you go

A
Alexander, 2014-03-19
@kryoz

var getCookie = function (name) {
  var matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}

D
Dmitry, 2015-07-13
@2fox

$que = mysql_query("SELECT * FROM friends WHERE idaut='$login' and frilog='$myrow[login]' and ban='1'");

and ban='1' is not needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question