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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:54:34+00:00 2026-05-14T20:54:34+00:00

This is the first time I am working for a front-end project that requires

  • 0

This is the first time I am working for a front-end project that requires server-side authentication for AJAX requests. I’ve encountered problems like I cannot make a call of session_start as the beginning line of the “destination page”, cuz that would get me a PHP Warning :

Warning: session_start() [function.session-start]: 
Cannot send session cache limiter -
headers already sent (output started at C:\xampp\htdocs\comic\app\ajaxInsert
    Book.php:1)
in C:\xampp\htdocs\comic\app\common.php  on line 10

I reckon this means I have to figure out a way other than checking PHP session variables to authenticate the “caller” of this PHP script, and this is my approach :

I have a “protected” PHP page, which must be used as the “container” of my javascript that posts the form through jQuery $.ajax(); method

In my “receiver” PHP script, what I’ve got is:

<?php
define(BOOKS_TABLE, "books");
define(APPROOT, "/comic/");
define(CORRECT_REFERER, "/protected/staff/addBook.php");

function isRefererCorrect()
{
    // the following line evaluates the relative path for the referer uri, 
    // Say, $_SERVER['HTTP_REFERER'] returns "http://localhost/comic/protected/staff/addBook.php"
    // Then the part we concern is just this "/protected/staff/addBook.php"
    $referer = substr($_SERVER['HTTP_REFERER'], 6 + strrpos($_SERVER['HTTP_REFERER'], APPROOT));
    return (strnatcmp(CORRECT_REFERER, $referer) == 0) ? true : false;
}

//http://stackoverflow.com/questions/267546/correct-http-header-for-json-file
header('Content-type: application/json charset=UTF-8');
header('Cache-Control: no-cache, must-revalidate');

echo json_encode(array
    (
        "feedback"=>"ok", 
        "info"=>isRefererCorrect()
    ));
?>

My code works, but I wonder is there any security risks in this approach? Can someone manipulate the post request so that he can pretend that the caller javascript is from the “protected” page?

UPDATE:

just realized I can let javascript from the secured page generate a unique token per ajax request, and use the passed token value to authenticate whether it is a “genuine ajax call” from the secured page

Will this be much better? Or should I just encrypt the content of the post request?

UPDATE AGAIN :

After two hours of looping through the included pages, I finally noticed that this weird situation was caused by my PHP page encoding…

I gave Notepad++ a try and carelessly chose the page encoding as UTF-8 with Byte Order Marker, so I kept getting the warning message due to the “weird” interpretation of this line:

<?php

A good lesson for me…

Many thanks to any hints or suggestions.

  • 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-14T20:54:34+00:00Added an answer on May 14, 2026 at 8:54 pm

    Technically seen, somebody can send you a request with referrer being 127.0.0.1, 0.0.0.0 or pretty much whatever they want without ever touching the site; so yeah, relying on it might not be the best approach.

    Also: You CAN and SHOULD use php_session – you simply have to call session_start(); BEFORE anything else sends ANYTHING (as the error clearly states: headers already sent). Judging from the error

    (output started at
    C:\xampp\htdocs\comic\app\ajaxInsert
    Book.php:1)

    this might simply be a newline at the beginning of the file i.e

    //empty line
    <?php
    //code
    

    Ad. Update:
    It’ll probably make things more secure; given the page which creates the token can only be accessed by people with the required permissions; it seems ok. Though even after reading your comment I believe you have some unneccessary whitespace somewhere, as sessions seem a perfectly fine way to authenticate the AJAX request.

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

Sidebar

Related Questions

This is my first time working with interfaces and proper namespace structure. When I
This is my first time attempting to call an ASP.NET page method from jQuery.
This is my first time using joomla. I don't know if I'm using the
This is my first time with Web services. I have to develop web services
This is a really basic question but this is the first time I've used
Been a while since I've dealt with ASP.NET and this is the first time
I have recently started learning F#, and this is the first time I've ever
I'm fairly new to deploying desktop applications, so this is the first time I'm
I saw this keyword for the first time and I was wondering if someone
problem euler #5 i found the solution but i don't know why this first

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.