1

Here is my code, I know that the SQL is returning a result because i have checked. However the set cookie does not seem to be working.

 public function login($username, $password){


    $username = $_POST['username'];
    $password = md5($_POST['password'].THEME_SALT);

    global $wpdb; 
    $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users WHERE username ='".$username."' AND password ='" .$password."' LIMIT 1");

    if (!empty($result)){

        setcookie("user", $username, time() + 3600);
    }
        return false;
}

Any ideas?

Planty
  • 85
  • 1
  • 12

3 Answers3

1

Try adding the path

`setcookie("user", $username, time() + 3600, '/');`
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • Thankyou, this worked! The $result that is returned contains data such as firstname and lastname. Is there a way that i could put this data into an array which is set in the cookie. I can then grab one element and say echo welcome $firstname? – Planty Jul 16 '15 at 09:37
  • @bri, i think you will get the answer here http://stackoverflow.com/questions/9032007/arrays-in-cookies-php – Shehary Jul 16 '15 at 09:43
  • If not, please ask new question for better answer or help. – Shehary Jul 16 '15 at 09:43
0

Try to use :

public function login($username, $password){

      $username = $_POST['username'];

       if ( username_exists( $username ) ){
           echo "Username In Use!";
            setcookie("user", $username, time() + 3600);
      } else{
               echo "Username Not In Use!";
      }
}
vrajesh
  • 2,935
  • 3
  • 25
  • 40
0

Since its in a function, I guess you try to use it after some output of your script. You should use it like any header before your output. If you turn on reporting of all errors you would get an error for this.

setcookie-manual @ php.net

SophieXLove64
  • 316
  • 1
  • 11