Title: What&#8217;s wrong with this code?
Last modified: August 18, 2016

---

# What’s wrong with this code?

 *  [nthnlsmmrs](https://wordpress.org/support/users/nthnlsmmrs/)
 * (@nthnlsmmrs)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/whats-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'];
       }
       else
       {
       //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.

## Tags

 * [cookie](https://wordpress.org/support/topic-tag/cookie/)
 * [id](https://wordpress.org/support/topic-tag/id/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [nthnlsmmrs](https://wordpress.org/support/users/nthnlsmmrs/)
 * Last activity: [18 years, 7 months ago](https://wordpress.org/support/topic/whats-wrong-with-this-code/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
