Support » Fixing WordPress » What’s wrong with this code?

  • The following code is supposed to work like this:
    1. Look for a cookie containing a referral user id#, if it exists, use it.
    2. Else if there is no cookie, look for a query with the user id#. If it exists, use it.
    3. Otherwise pick a random user id# and use it.
    4. regardless update the cookie and store the User ID variable to be used throughout the page.

    Why is this misbehaving? Right now, it only works every other time. it’s very annoying. I think it might also be breaking the php for the rest of the page. Though I’m not sure about that one.

    <?php                             //This is the affiliate user ID code
    $uid = 1;
    if($_COOKIE['ftw_uid'] != "")
    // user ID cookie found.
    $uid = $_COOKIE['uid'];
    elseif($_GET['uid'] != "")
    // no cookie found, but we found a query
    $uid = $_GET['ftw_uid'];
    //neither a cookie or a query found, use 1 or random uid
    global $wpdb;
    $rid = $wpdb->get_var("SELECT * FROM $wpdb->users ORDER BY RAND() LIMIT 1");
    //No matter what, update the cookie.
    setcookie("ftw_uid", $uid, time()+3000000);
    echo $uid;
  • The topic ‘What’s wrong with this code?’ is closed to new replies.