Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:52:36+00:00 2026-06-07T18:52:36+00:00

I run into a problem, and I appreciate if anyone can help me resolve

  • 0

I run into a problem, and I appreciate if anyone can help me resolve this.

Basically, I am making a code which allow user to add post to favorites, and save the favorite in cookie to remember it, but it fails. Here is my codes:

First, I use this code to run a function which check if user saved post or not:

<?php dt_fav_action(); ?>

And here is my dt_fav_action function:

function dt_fav_action() {
    global $post;
    $cookie = $_COOKIE['fav-'.$post->ID];
    if ($cookie == 1) {
        $user_faved = true;
    }
    if ($user_faved == true) {
        echo '<a href="';dt_favorites_link();echo '" class="faved">';dt_count_fav();echo '</a>';
    } else {
        echo '<a href="" class="addfav">';dt_count_fav();echo '</a>';
    }
}

And here is the $.ajax request which sends data to my php file to process the favorite and store the cookie etc.

$('.addfav').click(function(event){
    $(this).unbind('click');
    var parentfav = $(this).parent();
    var postid = $(this).parent().parent().parent().attr('id').replace(/[^0-9]/g, '');
    $(this).fadeOut();
    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/favorite.php',
        type: 'get',
        data: { postid : postid },
        dataType: 'json',
        success: function(data){
            parentfav.html('<a href="<?php dt_favorites_link(); ?>" class="faved">' + data.msg + '</a>');
            parentfav.children('.faved').css({'background-position' : 'left bottom', 'color' : '#1871a4'});
        }
    });
    return false;
});

And finally this is the favorite.php where I update the favorites count, and add cookie so it can be retrieved later via dt_fav_action function but here is the real problem, the $_COOKIE always returned null, it is not saved.

<?php

// include WordPress
require('../../../../wp-blog-header.php');
$data = array();

    // update post favorites count
    $postid = $_GET['postid'];
    update_post_meta($postid, 'faves', (int)get_post_meta($postid, 'faves', true)+1);
    $updated = get_post_meta($postid, 'faves', true);


    setcookie('fav-'.$postid, '1', time() + (20 * 365 * 24 * 60 * 60));

    $data['msg'] = $updated;

echo json_encode($data);

?>

setcookie part, I set the cookie when this is called via $.ajax and the script does not recognize any cookie after that, there fore it always fail and never remember the save state.

Please help .. I appreciate it in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:52:38+00:00Added an answer on June 7, 2026 at 6:52 pm

    Set cookie to be available on all child directories of the root directory.

    setcookie(
          'fav-'.$postid, 
          '1', 
          time() + (20 * 365 * 24 * 60 * 60), 
          "/"
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I run into this problem. I have a textarea which I only want to
I've run into another problem while fixing up my game to use this library.
I have run into a problem with generics in Java and I can't find
I knew this was a problem I would run into and I'm just not
I run into follow problem. Did I miss anything? Association.all().count() 1 Association.all().fetch(1) [Association(**{'server_url': u'server-url',
I just run into another problem with my To-do-like sortable list made with Knockout
I have run into a problem with Eclipse Indigo, attempting to start up my
I've just run into a problem with my ISP who it seems refuses to
I have run into a problem trying to modify a form I myself have
I have run into a problem w/ my model for databinding in WPF. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.