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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:58:26+00:00 2026-05-25T05:58:26+00:00

I am building an ajax/PHP script and I have come up with something to

  • 0

I am building an ajax/PHP script and I have come up with something to protect the ajax file from external access, by that I mean to restrict the execution of the script from another server (and domain). I am using JQuery $ajax to post to the PHP file.

Here is the PHP file:

 <?php
 $config["url"]="mysite.com";
 if (isset($_SERVER["HTTP_REFERER"])) {
     $url = parse_url($_SERVER["HTTP_REFERER"]);

     if ($url["host"] != $config["url"]) {
         echo "You don't have access to this file.";
         exit;
     } else { 
         //Run The script
     }
 }
 ?>

Basically what this script does is that it matches the Referer and the domain. The script will exit if it doesn’t match and will run if it does. So the script can be executed from only mysite.com and not from elsewhere.

I am not PHP / Javascript expert, so can anyone tell me if this is good or not and if it will fail under some conditions?

  • 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-05-25T05:58:27+00:00Added an answer on May 25, 2026 at 5:58 am

    What this script is protecting against is a Cross Site Request Forgery or CSRF. Checking the referer is one of the two primary defenses against this attack. The only problem is if the site has any user generated content, then they can add something such as:

    <img src="/src/to/your/ajax?params=something_evil" />
    

    which will still execute it.
    The second most common prevention method is using a CSRF token for ajax.
    Here are two resources to read up on to find more about this attack:

    Description: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)

    Prevention: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet

    Edit:

    A little clarification that may clear confusion on the subject.

    Any user can forge the referer header, but this is irrelevant. The reason it doesn’t matter is that if they can forge the referer, they can just as easily make a legitimate request.

    The important part here is ensuring that they cannot forge a request from any other user. This is the only aspect that matters and the only reason for checking the referer and protecting against CSRF attacks.

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

Sidebar

Related Questions

I have the following php script that is called by jquery ajax function report_range($time,
I'm working on building a PHP based proxy script to access a particular ASP.NET
So I'm building a web application and I have an ajax request that pings
I have a php script to retrieve data from a MysQL databse following an
I am building a comment system. so, I have a php page that displays
Interested in building my own drag'n'drop file uploader using JQuery/AJAX/PHP. Basically I want a
I have a script which calls a php file and gets the proper fields
I am building a site that uses a simple AJAX Servlet to talk JMS
I'm building a composite control that uses the telerik ajax controls as it's inputs,
I'm building a contact form (PHP file, design has been already made) which will

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.