Welcome Guest, Not a member yet? Register   Sign In
CORS policy Ci4
#1

(This post was last modified: 07-11-2021, 12:37 PM by paliz.)

i stuck with cors problem again my midddleware(filter) works fine on local host but on live host i got error 
PHP Code:
<?php namespace Modules\Common\Filters;

use 
CodeIgniter\config\Services;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\Response;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;


class 
CorsFilter implements FilterInterface
{
    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {


    }

    public function before(RequestInterface $request$arguments null)
    {


        // get origins
        if (array_key_exists('HTTP_ORIGIN'$_SERVER)) {
            $origin $_SERVER['HTTP_ORIGIN'];
        } else if (array_key_exists('HTTP_REFERER'$_SERVER)) {
            $origin $_SERVER['HTTP_REFERER'];
        } else {
            $origin $_SERVER['REMOTE_ADDR'];
        }
        $allowed_domains = array(
            'http://localhost:4200',
            'http://www.testerdemo.ir',
            'https://www.testerdemo.ir',
        );


        // this below code work on  localhost xammp server  localhost:8080


        if (in_array($origin$allowed_domains)) {
            header('Access-Control-Allow-Origin: ' $origin);
        }
// Cache-Control, Pragma
        header("Access-Control-Allow-Headers: Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token");
        header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS");
        header("Access-Control-Allow-Credentials: true");
//        header("Access-Control-Max-Age: 3600");
        //    header('content-type: application/json; charset=utf-8');
        $method $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            header("HTTP/1.1 200 OK");
            die();
        }

    }




i use this on localhost  but it is not working at all 
replace with ci4 htaacces?!(was good idea or should add to file)
Code:
## For CORS Configuration

    # Add Font Types
    AddType application/vnd.ms-fontobject .eot
    AddType application/x-font-ttf        .ttf
    AddType application/x-font-opentype  .otf
    AddType application/font-woff        .woff
    AddType application/font-woff2        .woff2

    <IfModule mod_headers.c>
        <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg|svgz|jpg|png|ico|font.css|css|js)$">
            ## un-remark this one for all access and remark out the one below it
            #Header set Access-Control-Allow-Origin "*"
            ## Change this to your local host url. and https or http
            Header add Access-Control-Allow-Origin: "http://localhost:4200"
            Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
            Header add Access-Control-Allow-Headers: "origin, x-api-key, x-requested-with, content-type, accept, access-control-request-method, access-control-allow-headers, authorization, observe, enctype, content-length, x-csrf-token"
            Header add Access-Control-Allow-Credentials true
        </FilesMatch>
    </IfModule>

    # Remove index.php from URL
#    RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
#    RewriteCond %{THE_REQUEST}              ^[^/]*/index\.php [NC]
#    RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]
# Force to request 200 for options

go to http://www.testerdemo.ir/
see in console

its full stack app angular 12 and ci4.1.3

CORS become pin in my ass

help me guys

like that its not working any way
# Remove index.php from URL
RewriteCond %{HTTP:X-Requested-With} !^XMLHttpRequest$
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.*)$ $1 [R=301,NS,L]
# Force to request 200 for options

help i m done with it
Enlightenment  Is  Freedom
Reply


Messages In This Thread
CORS policy Ci4 - by paliz - 07-11-2021, 12:31 PM
RE: CORS policy Ci4 - by includebeer - 07-12-2021, 07:19 AM
RE: CORS policy Ci4 - by paliz - 07-12-2021, 09:58 AM
RE: CORS policy Ci4 - by paliz - 07-12-2021, 11:01 AM
RE: CORS policy Ci4 - by paliz - 07-12-2021, 06:30 PM
RE: CORS policy Ci4 - by includebeer - 07-13-2021, 10:49 AM
RE: CORS policy Ci4 - by paliz - 07-13-2021, 11:58 AM
RE: CORS policy Ci4 - by paliz - 07-13-2021, 01:02 PM
RE: CORS policy Ci4 - by includebeer - 07-13-2021, 01:49 PM
RE: CORS policy Ci4 - by paliz - 07-13-2021, 01:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB