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 435149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:20:49+00:00 2026-05-12T20:20:49+00:00

Does anybody know of an automated way of telling whether a PHP script is

  • 0

Does anybody know of an automated way of telling whether a PHP script is being called directly (as a HTML page), or as a JavaScript, or as a CSS Stylesheet?

Without GET variables, or setting a flag in the file ( that is what I am doing right now).

Just curious.

EDIT: Some background because it was asked for in answers: The reason why I want this is a framework that I use when serving HTML pages as well as when serving CSS files. This frameweork has a custom error handler. When I’m in JS “mode”, I would like to throw errors as a JS alert(). When I’m in CSS mode, maybe a red body background or something. I would like to avoid working with flags (?mode=css) or constant definitions for the sake of code cleanness, but several answerers have confirmed that there is no “magic” way of finding out what a resource is being used for.

  • 1 1 Answer
  • 1 View
  • 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-05-12T20:20:50+00:00Added an answer on May 12, 2026 at 8:20 pm

    If I understand you correctly, you have a page which calls itself, (like this):

    <?php // page.php
    if (is_called_as_js()) {
        header('Content-Type: text/javascript;charset=utf-8');
        echo "alert('hello');";
        exit;
    } elseif (is_called_as_css()) {
        header('Content-Type: text/css');
        echo 'body { color: green }';
        exit;
    }
    ?>
    <html>
    (...)
    <script src="page.php"></script>
    <link rel="stylesheet" href="page.php" />
    

    In that case, no, there’s no way to tell – the browser sends a request saying GET /page.php. No intent is mentioned – just “give me the page and the browser will decide what to do with it”. (yeah, yeah, there is Accept and whatnot, haven’t seen a modern browser actually using this feature to say “give me this page as CSS”, most just say Accept: */*)

    If you insist that all your output, be it JS, CSS, or HTML, should be generated with one file, I suggest an URL rewriter (assuming Apache HTTP server, this would be mod_rewrite; most platforms offer this functionality in some way or another). Example using mod_rewrite:

    # .htaccess
    RewriteEngine On
    RewriteRule ^css/(.*) /page.php?type=css&file=$1 [L]
    RewriteRule ^js/(.*) /page.php?type=js&file=$1 [L]
    

    This way, request to /css/style.css will look like page.php?type=css&file=style.css when your script is run, similarly for /js/foobar.js.

    (Technically, you’re still using GET variables to find out if the result is supposed to be HTML,JS,or CSS; but it’s not visible to the users, plus you get around some older browsers’ limitation “if query string, don’t cache or cache brokenly”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know whether this can be achieved in jQuery. Target/addClass to the last
Does anybody know a way to enable response character set selection by client for
Does anybody know of a way in powermock you can unit test a constructor
Does anybody know a way I can return a user to my app after
Does anybody know a way to recursively remove all files in a working copy
Does anybody know a way to create a keyboard shortcut for the button 'Expand
Does anybody know why this function, when passed an invalid date (e.g. timestamp) to
Does anybody know if the CUDA library 'Thrust' can generate random numbers on the
Does anybody know how to programmatically disable the keyboard individual letter pop-up? (This blue
Does anybody know why my getAltitude in the following always returns 0? package com.example.helloandroid;

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.