-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
07-11-2021, 12:31 PM
(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 $request, ResponseInterface $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
-
includebeer CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
I think the problem comes from Google and not from your app. It says to register your domain for your client ID and to register this origin for this client ID...
My guess is when your test from localhost, Google let it pass because it's a dev environment. But on a live site they're more strict with security.
sign-in:1 Access to XMLHttpRequest at 'http://api.testerdemo.ir/public/api/auth/logout' from origin ' http://www.testerdemo.ir' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
api.testerdemo.ir/public/api/auth/logout:1 Failed to load resource: net::ERR_FAILED
2828907236-idpiframe.js:172 GET https://accounts.google.com/o/oauth2/iframerpc?action=checkOrigin&origin=http%3A%2F%2Fwww.testerdemo.ir&client_id=clientId 401
main.5ad2075b22ce5575afdc.js:1 {
details: "Not a valid origin for the client: http://www.testerdemo.ir has not been registered for client ID clientId. Please go to https://console.developers.google.com/ and register this origin for your project's client ID."
error: "idpiframe_initialization_failed"
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
07-12-2021, 09:58 AM
(This post was last modified: 07-12-2021, 10:47 AM by paliz.)
thank you
i find another way i hope work on host too
combine filter with htaccess
work 100% on local host
here code
got root document , public folder then change htaccess
PHP Code: # Disable directory browsing Options All -Indexes
# ---------------------------------------------------------------------- # Rewrite engine # ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features. # FollowSymLinks must be enabled for this to work. <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to # change the following line to match the subfolder you need. # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase # RewriteBase /
# Redirect Trailing Slashes... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com" RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Checks to see if the user is attempting to access a valid file, # such as an image or css document, if this isn't true it sends the # request to the front controller, index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
# add this bellow <IfModule mod_headers.c>
# 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
# <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>
<IfModule !mod_rewrite.c> # If we don't have mod_rewrite installed, all 404's # can be sent to index.php, and everything works as normal. ErrorDocument 404 index.php </IfModule>
# Disable server signature start ServerSignature Off # Disable server signature end
change corsfilter like bellow
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 $request, ResponseInterface $response, $arguments = null) {
}
public function before(RequestInterface $request, $arguments = null) { $method = $_SERVER['REQUEST_METHOD']; if ($method == "OPTIONS") { header("HTTP/1.1 200 OK"); die(); }
}
}
it test by me and got new error
Code: main.5ad2075b22ce5575afdc.js:1 ERROR w {headers: h, status: 200, statusText: "OK", url: "http://api.testerdemo.ir/public/api/auth/logout", ok: false, …}error: error: SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.o (http://www.testerdemo.ir/main.5ad2075b22ce5575afdc.js:1:44368)
at l.invokeTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:7136)
at Object.onInvokeTask (http://www.testerdemo.ir/main.5ad2075b22ce5575afdc.js:1:150200)
at l.invokeTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:7057)
at t.runTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:2532)
at u.invokeTask [as invoke] (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:8186)
at _ (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:20119)
at XMLHttpRequest.k (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:20443)message: "Unexpected token < in JSON at position 0"stack: "SyntaxError: Unexpected token < in JSON at position 0\n at JSON.parse (<anonymous>)\n at XMLHttpRequest.o (http://www.testerdemo.ir/main.5ad2075b22ce5575afdc.js:1:44368)\n at l.invokeTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:7136)\n at Object.onInvokeTask (http://www.testerdemo.ir/main.5ad2075b22ce5575afdc.js:1:150200)\n at l.invokeTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:7057)\n at t.runTask (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:2532)\n at u.invokeTask [as invoke] (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:8186)\n at _ (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:20119)\n at XMLHttpRequest.k (http://www.testerdemo.ir/polyfills.e9e5e2a9498904dca6fa.js:1:20443)"__proto__: Errortext: "<br />\n<b>Parse error</b>: syntax error, unexpected ':', expecting '{' in <b>/home3/esterdem/api.testerdemo.ir/system/Common.php</b> on line <b>49</b><br />\n"__proto__: Objectheaders: hlazyInit: ()=> {…}lazyUpdate: nullnormalizedNames: Map(0) {}__proto__: Objectmessage: "Http failure during parsing for http://api.testerdemo.ir/public/api/auth/logout"name: "HttpErrorResponse"ok: falsestatus: 200statusText: "OK"url: "http://api.testerdemo.ir/public/api/auth/logout"__proto__: q
work fine on local host but on live host still not working
shit hillllllllllllll
Enlightenment Is Freedom
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
When try go to sub domain
Api. Testerdemo.ir/public
Show this error
Parse error: syntax error, unexpected ':', expecting '{' in /home3/esterdem/api.testerdemo.ir/system/Common.php on line 49
I see the file its about time
!?
Enlightenment Is Freedom
-
includebeer CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
Check you log files, there must be a more detailed error message there, or test you api with postman. Now you only have a json parsing error, probably because the backend output an error message in html.
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
yes i used post man to develop back end then start to code for angular
i ll see my code could be any echo or print_r() in my php files
Enlightenment Is Freedom
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
i dont think my code is wrong
i download ci4 and upload it to host
it s empty project with home ctl thats all
http://api.testerdemo.ir/public/
show this error again
Parse error: syntax error, unexpected ':', expecting '{' in /home3/esterdem/api.testerdemo.ir/system/Common.php on line 49
some thing went rong with ci4
there s no filer just pure ci4 project
i change a few option in app config
PHP Code: public $baseURL = 'http://www.api.testerdemo.ir';
public $indexPage = '';
reset its same
Enlightenment Is Freedom
-
includebeer CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
You can't just guess what the error is with just this message. Look at your log files (apache and in CI's log), and look what response you get when you make that request with Postman.
And while you're at it, why not look if the file Common.php was uploaded correctly...
What version of CI is this?
-
paliz Member
  
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
07-13-2021, 01:53 PM
(This post was last modified: 07-13-2021, 01:54 PM by paliz.)
opppppppppppppsssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
my bad
i find why got wired errors on live host i set wrong php version 7.4 it must be 7.3
that was all that sub domain went wrong
i fucked up for choose wrong proper version
the whole time i search and change setting dig in stack over flew i could not see what was right version for ci4
ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
my local host version is v8.x but host only support v5 .x~ 7.x
my tine mistake almost kill me for couple of days
now i pass cors
second htaccess and filter works fine
Enlightenment Is Freedom
|