1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to retrieve this cookie value

Discussion in 'PHP' started by ketting00, Oct 15, 2014.

  1. #1
    Hi guys,
    I would ask question directly. When a user log in I setup these values.
    $_SESSION['name']     = $user['name'];
    
    $cookie_time = (3600 * 24 * 15); // 15 days
    setcookie ("name", $_SESSION['name'], time() + $cookie_time);
    Code (markup):
    The $_SESSION['name'] works as expected. But when I close a browser without logout. The next day I revisit my site and expect the cookie should work, but it's not working.

    The code I use to retrieve the cookie value is:
    $name = $_SESSION['name'] || $_COOKIE['name'];
    Code (markup):
    What's wrong with this?

    How do I retrieve the cookie?
    Thank you.
     
    Solved! View solution.
    ketting00, Oct 15, 2014 IP
  2. #2
    replace the line with the following:
    
    $name = (!empty($_SESSION['name'])) ? $_SESSION['name'] : ((isset($_COOKIE['name']) ? $_COOKIE['name'] : '');
    
    Code (markup):
    That should make it collect the cookie if the session isn't set
     
    PoPSiCLe, Oct 15, 2014 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Thanks @PoPSiCLe

    It works.

    Can you tell me where can I learn this coding style of a ? a : b ? b from.
    What is it called?
     
    ketting00, Oct 15, 2014 IP
  4. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #4
    ketting00, Oct 15, 2014 IP
  5. Daniel Hood

    Daniel Hood Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    Digital Goods:
    2
    #5
    http://davidwalsh.name/php-shorthand-if-else-ternary-operators has more information about it.

    [edit] apparently .name domains don't auto link.
     
    Daniel Hood, Oct 15, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    It's known as a ternary operator, or a ternary statement, as mention in the above post. It's a shorthand way to express very simple structures (like most if/else-statements), and can also be nested (although it's not really recommended).
     
    PoPSiCLe, Oct 15, 2014 IP