помогите создать для каждой страницы уникальное значение cookie

Discussion in 'PHP' started by ufaclub, 23 Oct 2012.

  1. ufaclub

    ufaclub New Member

    Joined:
    13 Jul 2010
    Messages:
    204
    Likes Received:
    0
    Reputations:
    0
    есть страница с урл который можно определить вот так: $url = $_SERVER['REQUEST_URI'];

    Есть переменная $price в которой случайно число из диапазона от 40 000 до 80 000;

    Dот мне нужно выводить на странице echo $price; чтобы эта переменная хранилась неделю.

    Cуть в том что у каждой страницы $price определятся случайным образом и сохраняется у пользователя в куках на неделю. Т.е он открыв эту же страницу увидит число из переменной которая хранится в куках.

    как сделать?
     
  2. b3

    b3 Moderator

    Joined:
    5 Dec 2004
    Messages:
    2,041
    Likes Received:
    933
    Reputations:
    199
    Нереально просто сложно сделать $_COOKIE['price'] = $price;
    PS только делай через setcookie
     
    _________________________
  3. ufaclub

    ufaclub New Member

    Joined:
    13 Jul 2010
    Messages:
    204
    Likes Received:
    0
    Reputations:
    0

    а на примере можно? вот есть

    $url = $_SERVER['REQUEST_URI'];
    $price = rand(40000, 80000);
     
  4. trololoman96

    trololoman96 Elder - Старейшина

    Joined:
    1 Dec 2011
    Messages:
    120
    Likes Received:
    34
    Reputations:
    55
    b3, разве куки пропусуются не для домена в целом ? При переходе на следующую страницу price просто перепишется же.

    ufaclub, предлагаю такой вариант - при заходе на страницу создать сереализованный массив с id страницы и price и записать его в куки, ну или сессию. Потом если массив уже есть просто дополнять его.
     
  5. b3

    b3 Moderator

    Joined:
    5 Dec 2004
    Messages:
    2,041
    Likes Received:
    933
    Reputations:
    199
    Куки можно назначить не на домен в целом а на директорию, в таком случае можно совместить с мод_реврайт и выдавать каждую страницу как будто в отдельной директории. Как вариант еще $price тоже в урл запихнуть через мод_реврайт + куки.
    На выходе получим адреса вида:
    www.site.com/page/911911/
    Вот переменная $price = 911911
    name = price
    expire = time()+60*60*24*7
    path = /page/911911
    value = 911911

    Есть загвоздка, если страница одна, но каждому пользователю нужно выдавать разную цену, тогда все очень просто и даже мод реврайт не нужен. Но если страниц много, тогда :

    RFC2109

    Code:
    6.3  Implementation Limits
    
       Practical user agent implementations have limits on the number and
       size of cookies that they can store.  In general, user agents' cookie
       support should have no fixed limits.  They should strive to store as
       many frequently-used cookies as possible.  Furthermore, general-use
       user agents should provide each of the following minimum capabilities
       individually, although not necessarily simultaneously:
    
          * at least 300 cookies
    
          * at least 4096 bytes per cookie (as measured by the size of the
            characters that comprise the cookie non-terminal in the syntax
            description of the Set-Cookie header)
    
          * at least 20 cookies per unique host or domain name
    
       User agents created for specific purposes or for limited-capacity
       devices should provide at least 20 cookies of 4096 bytes, to ensure
       that the user can interact with a session-based origin server.
    
       The information in a Set-Cookie response header must be retained in
       its entirety.  If for some reason there is inadequate space to store
       the cookie, it must be discarded, not truncated.
    
       Applications should use as few and as small cookies as possible, and
       they should cope gracefully with the loss of a cookie.
     
    _________________________
  6. fl00der

    fl00der Moderator

    Joined:
    17 Dec 2008
    Messages:
    1,028
    Likes Received:
    310
    Reputations:
    86
    $_COOKIE['<имя_страницы>_price']=$price;
     
    _________________________
  7. b3

    b3 Moderator

    Joined:
    5 Dec 2004
    Messages:
    2,041
    Likes Received:
    933
    Reputations:
    199
    а если страниц 10000 ?
     
    _________________________
  8. Gifts

    Gifts Green member

    Joined:
    25 Apr 2008
    Messages:
    2,494
    Likes Received:
    807
    Reputations:
    614
    b3 преждевременная оптимизация - зло
     
    _________________________