Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a 404 not a 404?

I'm using this jQuery below call to load a .php file located on the same server.

However, using Chrome's javascript console, its reporting "404 not found" on the php file I'm trying to load. Although, I can load the file directly, just by clicking on the file right there from within the console.

Also, I can copy the URL of the file, right from the javascript console where it reports 404 (not found), open a new tab, paste it into the address bar, and hit the script fine, without issue.

Is this something specific to the jQuery get method? What could be causing the page to 404 in the get method but execute fine when called directly?

$('.colorReset').click
    (
        function() 
        {
        var myImage = $('#theme :selected').text();
        $.get('<?php echo get_bloginfo('template_directory') ?>/colorReset.php', {theme: myImage, spot: '1'}, function(data){doColor('#theme_header_color', data);});
        }
    );

    //script never gets to the doColor function, due to the apparent 404 on colorReset.php
function doColor(el, color)
    {
    $(el).val(color).trigger('keyup');
    $(el).attr('value', color);
    $(el).val(color);
}

Here is the javascript console result

I'm able to call the file directly using the same URL the console reports as a 404

Header Report

Here is the source file, colorReset.php, that's called by the get...

<?php
require_once('../../../wp-blog-header.php');

add_action( 'admin_init', 'check_user' );

function check_user()
    {
    if (!is_user_logged_in()){
        die("You Must Be Logged In to Access This");
    }
    if( ! current_user_can('edit_files')) {
        die("Oops sorry you are not authorized to do this");
    }
}

$myTheme = $_REQUEST['theme'];
$spot = $_REQUEST['spot'];
$myThemeColor = $myTheme."_color".$spot;

$file = "styles/".$myTheme."/template.ini";
    if (file_exists($file) && is_readable($file))
    {
    $ini_array = parse_ini_file($file);
     if($spot == 1){$myColor = $ini_array['color1'];}
     if($spot == 2){$myColor = $ini_array['color2'];}
     if($spot == 3){$myColor = $ini_array['color3'];}
     if($spot == 4){$myColor = $ini_array['color4'];}
    }
    else
    {
     if($spot == 1){$myColor = get_option('theme_header_color');}
     if($spot == 2){$myColor = get_option('theme_sidebar_color');}
     if($spot == 3){$myColor = get_option('theme_spot_color_alt');}
     if($spot == 4){$myColor = get_option('theme_spot_color_alt2');}
    }
echo $myColor;
?>
like image 563
Scott B Avatar asked Jun 01 '11 14:06

Scott B


1 Answers

As described in another answer, loading wp-blog-header.php bootstraps the entire WordPress request handling process. Given that your script isn't actually a WordPress post, this process sets the 404 header to indicate that it couldn't find the content you were looking for.

Since it looks like what you really want is just access to the WordPress user functions, you're better off just including wp-load.php which should allow you to call those functions without invoking the request parser.

like image 53
Tim Stone Avatar answered Sep 23 '22 15:09

Tim Stone