Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php cors

/**
 *  An example CORS-compliant method.  It will allow any GET, POST, or OPTIONS requests from any
 *  origin.
 *
 *  In a production environment, you probably want to be more restrictive, but this gives you
 *  the general idea of what is involved.  For the nitty-gritty low-down, read:
 *
 *  - https://developer.mozilla.org/en/HTTP_access_control
 *  - https://fetch.spec.whatwg.org/#http-cors-protocol
 *
 */
function cors() {
    
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
        exit(0);
    }
    
    echo "You have CORS!";
}
Comment

php cors

<?php
 header("Access-Control-Allow-Origin: *");
Comment

PHP Cors

/**
 *  An example CORS-compliant method.  It will allow any GET, POST, or OPTIONS requests from any
 *  origin.
 *
 *  In a production environment, you probably want to be more restrictive, but this gives you
 *  the general idea of what is involved.  For the nitty-gritty low-down, read:
 *
 *  - https://developer.mozilla.org/en/HTTP_access_control
 *  - https://fetch.spec.whatwg.org/#http-cors-protocol
 *
 */
function cors() {
    
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
        exit(0);
    }
    
    echo "You have CORS!";
}
Comment

php cors error

Access-Control-Allow-Headers does not allow * as accepted value, see the Mozilla Documentation here.

Instead of the asterisk, you should send the accepted headers (first X-Requested-With as the error says).
Comment

PREVIOUS NEXT
Code Example
Php :: onbeforeunload com mysql php 
Php :: how to check php version in cpanel 
Php :: php sort array 
Php :: why are my css properties not being applied to php file 
Php :: eloquentdatatable add column 
Php :: Problems with Special Characters between Angular http post and PHP 
Php :: php pdo multiple insert 
Php :: how to get many of quensation php programming language 
Php :: trying to change iframe location from javascript 
Php :: How to programmatically grab the product description in WooCommerce? 
Php :: laravel download file from AWS s3 
Php :: how to print image just on side where upload php 
Php :: laravel csrf token or protection or laravel form 
Php :: Laravel array to string error 
Php :: onde fica o php ini ubuntu 
Php :: factorial program in php 
Php :: laravel blade dynamic class loop foreach 
Php :: how to give dynamic value in onlick in php 
Php :: laravel-filemanager showing blank page 
Php :: Binance api buymarket php 
Php :: OroCRM Custom Bundle is loaded? 
Php :: how to disable html coding property in php 
Php :: laravel collection modelKeys 
Php :: odoctrine querybuilder print sql 
Php :: form alter drupal 9 language code 
Php :: saleor meaning 
Php :: ass 
Php :: Pasar el email de recuperar contraseña de laravel a español 
Php :: one to many laravel 
Php :: laravel run controller from command line 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =