0

I'm looking for to use a cookie with wordpress 4.0 and it doesn't work.( OVH) I have read that Session or Cookie are deactivate by default inside the load.php folder. Somebody could tell me how to create and use a cookie comparing a password from a form and a password from a database . "id = 0 " "password = sha1 of my password" Here is my script which doesn't work:

What is the simple way to do that?

Creation of cookie and compare the password

if(isset($_POST['password'])){
  mysql_connect("db","login","password");
  mysql_select_db("nameofdb");
  $sql = " SELECT * FROM authentification WHERE id=0 ";
  $req = mysql_query($sql);
  $recp = mysql_fetch_array($req);
  $result = $recp['password'];
  $pass = sha1($_POST['password']);

    $cookie_name = "mycookie" ;
    $cookie_value = $result; 
  setcookie($cookie_name, $cookie_value,time()+3600*24, "/",urlwebsite);

  if( $pass == $result){

        wp_redirect('url to protected page '); 
    }
    else{
   echo "Wrong password - try again";
    }
}   
?>

Use the cookie to protect the content

if(isset($cook) and $cook == $result){
    mysql_connect("db","login","password");
    mysql_select_db("nameofdb");

        $sql = " SELECT * FROM authentification WHERE id=0 ";
    $req = mysql_query($sql);
    $recp = mysql_fetch_array($req);
    $result = $recp['password'];


?>

Content to be protected

<?php
}
else{
    wp_redirect('url : return to the authentication form page');
  exit();
}

?>

This script doesn't work, the cookie isn't sent to the private page . My authenticate page works because the cookie is well created but it didn't send the cookie to the private page

Here is my new code. Doesn't work :

if(isset($_POST['password'])){
  global $wpdb;
  $sql = $wpdb->get_results('SELECT * FROM authentification WHERE id=0');
  foreach ($sql as $myvar) {
        $result = $myvar->password;
    }
  $pass = sha1($_POST['password']);

  $cookie_name = "mycookie" ;
    $cookie_value = $result; 
  setcookie($cookie_name, $cookie_value,time()+3600*24,COOKIEPATH, COOKIE_DOMAIN);

  if( $pass == $result){

        wp_redirect('url to protected page'); 
    }
    else{
   echo "Mauvais mot de passe - réessayer";
    }
}

and the protected page:

<?php
$cook = $_COOKIE['mycookie'];
echo $cook;

        global $wpdb;
        $sql = $wpdb->get_results('SELECT * FROM authentification WHERE id=0');
        foreach ($sql as $myvar) {
            $result = $myvar->password;
        }


    $req = $wpdb->query($sql);
    $recp = $wpdb->get_col( $req, 1 );
    $result = echo $recp;
*/

if(isset($cook) and $cook == $result){

/*}
else{
    wp_redirect('url to authenticate form');
  exit();
}
*/
?>  
  • 1
    edit your question and add more detail about how exactly it "doesn't work". enable debugging and note any errors. there is nothing in WordPress that prevents you from using cookies or sessions if done properly. also, don't use mysql_ functions, use wpdb. – Milo Nov 18 '14 at 1:03
  • Your first advice is very helpful. should we use wordpress functions to operate cookies and what are they. The result of my script is that the cookie is not send to the page to protect . – Raphael Kuhn Nov 18 '14 at 8:36
  • with this code it doesn't work:global $wpdb; $sql = $wpdb->get_results('SELECT * FROM $wpdb->authentification WHERE id=0'); foreach ($sql as $myvar) { $result = $myvar->password; } echo $result; – Raphael Kuhn Nov 18 '14 at 9:45
  • unless you explicitly set $wpdb->authentification, that property doesn't exist. that only works for native tables, you have to otherwise specify the table names in queries. there are no WordPress functions to work with cookies, you just use the regular php functions. are you getting php errors? are you trying to set cookies after headers are sent? and edit your question to add more detail, don't put it in comments where people have to search for it to help you. – Milo Nov 18 '14 at 16:00
  • @Milo, i do that. I just want to know if my php code is good ? – Raphael Kuhn Nov 18 '14 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.