request IP in Laravel application not correct - php

in laravel when I want get visitor IP from request()->getClientIp() it always show my server IP and offcurse it not correct, I read about this problem I think it's related to cloudflare and
laravel TrustProxies but I don't know what should I do.
how can I get real visitor IP address?
here is my $_Server dump
array:42 [
"REDIRECT_STATUS" => "200"
"HTTP_X_FORWARDED_HOST" => "dpaper.ir"
"HTTP_X_FORWARDED_PROTO" => "https"
"HTTP_X_FORWARDED_PORT" => "443"
"HTTP_X_FORWARDED_FOR" => "::ffff:46.225.69.114"
"HTTP_COOKIE" => "_ga=GA1.2.199690116.1583749582; _gid=GA1.2.452860032.1584212542; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6ImhYWmc5OW4zTktlakRhWDRYTmpGNmc9P"
"HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9,fa;q=0.8,th;q=0.7"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate, br"
"HTTP_SEC_FETCH_USER" => "?1"
"HTTP_SEC_FETCH_MODE" => "navigate"
"HTTP_SEC_FETCH_SITE" => "none"
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
"HTTP_USER_AGENT" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
"HTTP_UPGRADE_INSECURE_REQUESTS" => "1"
"HTTP_CONNECTION" => "keep-alive"
"HTTP_HOST" => "dpaper.ir"
"PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
"SERVER_SIGNATURE" => "<address>Apache/2.4.25 (Debian) Server at dpaper.ir Port 80</address>"
"SERVER_SOFTWARE" => "Apache/2.4.25 (Debian)"
"SERVER_NAME" => "dpaper.ir"
"SERVER_ADDR" => "10.11.172.198"
"SERVER_PORT" => "80"
"REMOTE_ADDR" => "10.11.81.123"
"DOCUMENT_ROOT" => "/var/www/html/public"
"REQUEST_SCHEME" => "http"
"CONTEXT_PREFIX" => ""
"CONTEXT_DOCUMENT_ROOT" => "/var/www/html/public"
"SERVER_ADMIN" => "webmaster#localhost"
"SCRIPT_FILENAME" => "/var/www/html/public/index.php"
"REMOTE_PORT" => "35058"
"REDIRECT_URL" => "/testi"
"GATEWAY_INTERFACE" => "CGI/1.1"
"SERVER_PROTOCOL" => "HTTP/1.1"
"REQUEST_METHOD" => "GET"
"QUERY_STRING" => ""
"REQUEST_URI" => "/testi"
"SCRIPT_NAME" => "/index.php"
"PHP_SELF" => "/index.php"
"REQUEST_TIME_FLOAT" => 1584720819.106
"REQUEST_TIME" => 1584720819
"argv" => []
"argc" => 0
]

I found a solution for this problem, for fix the problem you should put * in TrustProxies Middleware
like this:
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* #var array|string
*/
protected $proxies = '*';
/**
* The headers that should be used to detect proxies.
*
* #var int
*/
protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

Related

Logged in with web route but can not authenticated with api route

Duplicated: Laravel using web authentication in all api routes redirect to home
I don't know the keyword to search it on google so sorry about something stupid. I use web route to load view and api route to load json data like this:
/shop/categories
/api/shop/categories
I logged in with web route and auth middleware check is okay, I can get user name by auth()->user()->name. But when I call jquery ajax with /api/shop/categories I get an error "Authenticated.", I try to add middleware auth:api but nothing change. I read document and I see I must include api_token url.
How do I use the api when logged in with web route, middleware check?
Here is my some code:
//web route
Route::group(['prefix' => $prefix, 'middleware' => ['auth']], function() {
Route::get('categories', 'CategoryController#getView')->name('category_get_view');
});
// api route
Route::group(['prefix' => $prefix, 'middleware' => ['auth:api']], function() {
Route::get('categories', 'CategoryController#getJson');
Route::get('categories/{category}', 'CategoryController#getDetail');
Route::put('categories/{category}', 'CategoryController#putSave');
Route::post('categories', 'CategoryController#postSave');
Route::delete('categories/{category}', 'CategoryController#deleteSave');
});
// dd($request)
Request {#42
#json: null
#convertedFiles: null
#userResolver: Closure {#399
class: "Illuminate\Auth\AuthServiceProvider"
this: AuthServiceProvider {#32 …}
parameters: {
$guard: {
default: null
}
}
use: {
$app: Application {#2 …}
}
file: "\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
line: "85 to 87"
}
#routeResolver: Closure {#412
class: "Illuminate\Routing\Router"
this: Router {#25 …}
use: {
$route: Route {#181 …}
}
file: "\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
line: "649 to 651"
}
+attributes: ParameterBag {#44
#parameters: []
}
+request: ParameterBag {#50
#parameters: array:2 [
"key" => null
"page" => "1"
]
}
+query: ParameterBag {#50}
+server: ServerBag {#46
#parameters: array:40 [
"REDIRECT_STATUS" => "200"
"HTTP_HOST" => "backend.local"
"HTTP_CONNECTION" => "keep-alive"
"HTTP_ACCEPT" => "*/*"
"HTTP_X_CSRF_TOKEN" => "BNlGYoppmtl7fljKeQ4TGdNmbGoiuKWTeoLDbKln"
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
"HTTP_REFERER" => "http://backend.local/shop/categories"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate"
"HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9,ja;q=0.8"
"HTTP_COOKIE" => "XSRF-TOKEN=eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9; laravel_session=eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0%3D"
"PATH" => ""
"SystemRoot" => "C:\WINDOWS"
"COMSPEC" => "C:\WINDOWS\system32\cmd.exe"
"PATHEXT" => ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
"WINDIR" => "C:\WINDOWS"
"SERVER_SIGNATURE" => "<address>Apache/2.4.39 (Win64) PHP/7.2.18 Server at backend.local Port 80</address>\n"
"SERVER_SOFTWARE" => "Apache/2.4.39 (Win64) PHP/7.2.18"
"SERVER_NAME" => "backend.local"
"SERVER_ADDR" => "::1"
"SERVER_PORT" => "80"
"REMOTE_ADDR" => "::1"
"DOCUMENT_ROOT" => "/public"
"REQUEST_SCHEME" => "http"
"CONTEXT_PREFIX" => ""
"CONTEXT_DOCUMENT_ROOT" => "/public"
"SERVER_ADMIN" => "wampserver#wampserver.invalid"
"SCRIPT_FILENAME" => "/public/index.php"
"REMOTE_PORT" => "64525"
"REDIRECT_URL" => "/api/shop/categories"
"REDIRECT_QUERY_STRING" => "key=&page=1"
"GATEWAY_INTERFACE" => "CGI/1.1"
"SERVER_PROTOCOL" => "HTTP/1.1"
"REQUEST_METHOD" => "GET"
"QUERY_STRING" => "key=&page=1"
"REQUEST_URI" => "/api/shop/categories?key=&page=1"
"SCRIPT_NAME" => "/index.php"
"PHP_SELF" => "/index.php"
"REQUEST_TIME_FLOAT" => 1570505297.533
"REQUEST_TIME" => 1570505297
]
}
+files: FileBag {#47
#parameters: []
}
+cookies: ParameterBag {#45
#parameters: array:2 [
"XSRF-TOKEN" => "eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9"
"laravel_session" => "eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0="
]
}
+headers: HeaderBag {#48
#headers: array:10 [
"host" => array:1 [
0 => "backend.local"
]
"connection" => array:1 [
0 => "keep-alive"
]
"accept" => array:1 [
0 => "*/*"
]
"x-csrf-token" => array:1 [
0 => "BNlGYoppmtl7fljKeQ4TGdNmbGoiuKWTeoLDbKln"
]
"x-requested-with" => array:1 [
0 => "XMLHttpRequest"
]
"user-agent" => array:1 [
0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
]
"referer" => array:1 [
0 => "http://backend.local/shop/categories"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate"
]
"accept-language" => array:1 [
0 => "en-US,en;q=0.9,ja;q=0.8"
]
"cookie" => array:1 [
0 => "XSRF-TOKEN=eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9; laravel_session=eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0%3D"
]
]
#cacheControl: []
}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/shop/categories"
#requestUri: "/api/shop/categories?key=&page=1"
#baseUrl: ""
#basePath: null
#method: "GET"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
Maybe you are missing Authorization in your request's header..
if you want to get data from authenticated users but want it from the api authenticated. You should get it using auth('guard-name')

Bad Request, Missing required parameters Yii2

I have some problem with GET request.
If I send GET request like a /api/v1/users/user-settings?key=model-manual-user-settings in my app,
I get response like this:
{"name":"Bad Request","message":"Missing required parameters: key", "code":0, "status":400, "type":"yii\\web\\BadRequestHttpException"}
Run method in my action
/**
* Получение пользовательских данных по ключу
*
* #param string $key Ключ пользовательских параметров
* #return array
*/
public function run($key)
{
$component = new UserSettingsComponent($key);
return $component->getSettingsWithParams();
}
Piece of yii2 log file:
2018-05-25 18:58:52 [127.0.0.1][1][tsdsfjgmllh43vfb264qimc3fv][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Missing required parameters: key in /var/www/arm/vendor/yiisoft/yii2/web/Controller.php:149
Stack trace:
#0 /var/www/arm/vendor/yiisoft/yii2/base/Action.php(88): yii\web\Controller->bindActionParams(Object(api\actions\users\user_settings\IndexAction), Array)
#1 /var/www/arm/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\Action->runWithParams(Array)
#2 /var/www/arm/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('index', Array)
#3 /var/www/arm/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('v1/users/user-s...', Array)
#4 /var/www/arm/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#5 /var/www/arm/api/web/index.php(15): yii\base\Application->run()
#6 {main}
2018-05-25 18:58:52 [127.0.0.1][1][tsdsfjgmllh43vfb264qimc3fv][info][application] $_GET = [
'args' => ''
]
...
$_SERVER = [
'USER' => 'www-data'
'HOME' => '/var/www'
'HTTP_X_COMPRESS' => 'null'
'HTTP_COOKIE' => 'PHPSESSID=tsdsfjgmllh43vfb264qimc3fv; _csrf=f70915c9bfa9e77535bd94fa3287cb4347f248c120e26332eca5a1cebc7dba4ca%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22mNhgJJk1mrM_luk3c26x-dBOVUY62fpo%22%3B%7D'
'HTTP_ACCEPT_LANGUAGE' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
'HTTP_UPGRADE_INSECURE_REQUESTS' => '1'
'HTTP_CACHE_CONTROL' => 'max-age=0'
'HTTP_CONNECTION' => 'keep-alive'
'HTTP_HOST' => 'arm.local'
'REDIRECT_STATUS' => '200'
'SERVER_NAME' => 'arm.local'
'SERVER_PORT' => '80'
'SERVER_ADDR' => '127.0.0.1'
'REMOTE_PORT' => '42354'
'REMOTE_ADDR' => '127.0.0.1'
'SERVER_SOFTWARE' => 'nginx/1.10.3'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'REQUEST_SCHEME' => 'http'
'SERVER_PROTOCOL' => 'HTTP/1.1'
'DOCUMENT_ROOT' => '/var/www/arm'
'DOCUMENT_URI' => '/api/web/index.php'
'REQUEST_URI' => '/api/v1/users/user-settings?key=wagons-array-user-settings'
'SCRIPT_NAME' => '/api/web/index.php'
'CONTENT_LENGTH' => ''
'CONTENT_TYPE' => ''
'REQUEST_METHOD' => 'GET'
'QUERY_STRING' => 'args'
'SCRIPT_FILENAME' => '/var/www/arm/api/web/index.php'
'FCGI_ROLE' => 'RESPONDER'
'PHP_SELF' => '/api/web/index.php'
'REQUEST_TIME_FLOAT' => 1527274732.2266
'REQUEST_TIME' => 1527274732
]
Url manager config:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'/settings' => '/settings',
'/<action>' => '/site/<action>',
'/<controller>' => '/<controller>/<action>',
'/<controller>/<action>' => '/<controller>/<action>',
'/<module>/<action>' => '/<module>/default/<action>',
[
'class' => 'yii\rest\UrlRule',
'controller' => [
# some controllers
'v1/users/user-groups',
'v1/users/user-settings',
],
'pluralize'=>false
],
],
]
section of nginx config
location /api/ {
root /var/www/arm/api/web;
try_files $uri /api/web/index.php?args;
}
Also I see these lines in my nginx log file:
2018/05/25 21:56:21 [error] 27040#27040: *74 FastCGI sent in stderr: "PHP message: PHP Warning: Invalid argument supplied for foreach() in /var/www/arm/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php on line 123" while reading response header from upstream, client: 127.0.0.1, server: arm.local, request: "GET /api/v1/users/user-settings?key=wagons-array-user-settings HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "arm.local"
2018/05/25 21:58:52 [error] 27040#27040: *80 FastCGI sent in stderr: "PHP message: PHP Warning: Invalid argument supplied for foreach() in /var/www/arm/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php on line 123" while reading response header from upstream, client: 127.0.0.1, server: arm.local, request: "GET /api/v1/users/user-settings?key=wagons-array-user-settings HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "arm.local"
Note
It's began after update yii2 to 2.0.15.1 and php 7.2.5.
I try downgrade to php7.2.3, problem still exists.
Production version my app used yii2 v2.0.13 and php 7.2.3 and I haven`t this problem.
If I downgrade yii2 version to 2.0.13 I get this error:
yiisoft/yii2-gii 2.0.7 requires yiisoft/yii2 ~2.0.14 -> satisfiable by yiisoft/yii2[2.0.x-dev]
resp. if I try downgrade yiisoft/yii2-gii to v2.0.0 I get this error:
The requested package yiisoft/yii2-gii (locked at 2.0.7, required as 2.0.0) is satisfiable by yiisoft/yii2-gii[2.0.7]
server nginx + php-fpm
Yii version 2.0.15.1
php version 7.2.5-1
nginx version 1.10
Someone have any ideas?
You have a typo in your nginx configuration, you missed $ before args:
location /api/ {
root /var/www/arm/api/web;
try_files $uri /api/web/index.php?$args;
}

Yii2 redirecting isn't working. Why?

I am newbie in Yii2, and now I build new app manually (using Mark Safronov book).
I tried to redirect my controller to somewhere and it isn't working.
Like this:
return $this->redirect(array('/site/dashboard'));
I think it depends on my settings, do you have any ideas? I'm using niceurls and this is my httaccess file:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
It is my View code
And I had next config: common/config/bootstrap.php
Yii::setAlias('yii2_book', dirname(dirname(__DIR__)) . '/yii2_book');
Code in my controller:
public function actionAdd()
{
$customer = new CustomerRecord();
$phone = new PhoneRecord();
if($customer->load(Yii::$app->request->post()) && $customer->validate() && $phone->load(Yii::$app->request->post())){
$this->store($this->makeCustomer($customer, $phone));
return $this->redirect(['index']);
}
return $this->render('add',compact('customer','phone'));
Stack trace:
2018-01-05 11:30:03 [::1][-][-][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Unable to verify your data submission. in /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/web/Controller.php:166
Stack trace:
#0 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Controller.php(155): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#1 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('add', Array)
#2 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('customers/add', Array)
#3 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#4 /Users/sergejandrejkin/PhpstormProjects/yii2_book/web/index.php(12): yii\base\Application->run()
#5 {main}
2018-01-05 11:30:03 [::1][-][-][info][application] $_POST = [
'_csrf' => 'oUDxUGHONyj8ER3vQc9cvdpMsCJh6DXlPzsnxRCxfu3AArIlA7lYR8tnbN8UuDf_937iFlKnc6tGVhCNQvQKiA=='
'CustomerRecord' => [
'name' => ''
'birth_date' => ''
'notes' => ''
]
'PhoneRecord' => [
'number' => ''
]
]
$_SERVER = [
'REDIRECT_STATUS' => '200'
'HTTP_HOST' => 'localhost:8888'
'HTTP_CONNECTION' => 'keep-alive'
'CONTENT_LENGTH' => '208'
'HTTP_CACHE_CONTROL' => 'max-age=0'
'HTTP_ORIGIN' => 'http://localhost:8888'
'HTTP_UPGRADE_INSECURE_REQUESTS' => '1'
'CONTENT_TYPE' => 'application/x-www-form-urlencoded'
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
'HTTP_REFERER' => 'http://localhost:8888/web/customers/add'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, br'
'HTTP_ACCEPT_LANGUAGE' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
'PATH' => '/usr/bin:/bin:/usr/sbin:/sbin'
'SERVER_SIGNATURE' => ''
'SERVER_SOFTWARE' => 'Apache/2.2.31 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.0.15 mod_ssl/2.2.31 OpenSSL/1.0.2j DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0'
'SERVER_NAME' => 'localhost'
'SERVER_ADDR' => '::1'
'SERVER_PORT' => '8888'
'REMOTE_ADDR' => '::1'
'DOCUMENT_ROOT' => '/Users/sergejandrejkin/PhpstormProjects/yii2_book'
'SERVER_ADMIN' => 'you#example.com'
'SCRIPT_FILENAME' => '/Users/sergejandrejkin/PhpstormProjects/yii2_book/web/index.php'
'REMOTE_PORT' => '49253'
'REDIRECT_URL' => '/web/customers/add'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'SERVER_PROTOCOL' => 'HTTP/1.1'
'REQUEST_METHOD' => 'POST'
'QUERY_STRING' => ''
'REQUEST_URI' => '/web/customers/add'
'SCRIPT_NAME' => '/web/index.php'
'PHP_SELF' => '/web/index.php'
'REQUEST_TIME_FLOAT' => 1515148203.45
'REQUEST_TIME' => 1515148203
'argv' => []
'argc' => 0
]
I've had same issue. Considering store(…) method works fine, try to add Yii::$app->end() after redirect. So
return $this->redirect(['index']);
becomes
$this->redirect(['index']);
Yii::$app->end();
The exception says:
yii\web\BadRequestHttpException: Unable to verify your data submission.
This tell you 2 things:
bad request exception (http code 400): Is a server response that is sent when the request done by the browser is invalid. Checking the exception's stacktrace you can see its thrown in the beforeAction of the actionAdd so your redirect isnt the problem, as the exception is before your code.
Yii usually use this exception when the CSRF check is invalid, so you should check this page
check it out , do you have any echo before that ?
if have just clear it by ob_end_clean();
if any echo or header have exist before redirect , it's wont be work !
try :
flush();
or try
ob_start()
if no one works try javascript method :
echo "<script type='text/javascript'> window.location='http://your complete url .. '; </script>";
that will works 100% ,good luck .
Yes please check your config settings.
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
],
]
Also your mod_rewrite module should be enabled.
Hope it helps!

Asana upload error with Asana/php-asana

trying to test provided sample code:
$attachment = $client->attachments->createOnTask(
$enquiry->id,
'hello word',
'upload.txt',
'text/plain'
);
Error:
400 File is not an object
The client definitely works (able to create task just before).
The taskId ($enquiry->id) is correct (checked by going to the url etc).
Using v0.4.0 of the library and PHP 5.6.18-1 off a Homestead virtual machine.
Similar questions on SO talk about bad encoding but everything seems ok in my case.
Below is the response:
class Httpful\Response#961 (13) {
public $body =>
class stdClass#964 (1) {
public $errors =>
array(1) {
[0] =>
class stdClass#973 (2) {
...
}
}
}
public $raw_body =>
string(219) "{"errors":[{"message":"file: File is not an object","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}"
public $headers =>
class Httpful\Response\Headers#963 (1) {
private $headers =>
array(11) {
'server' =>
string(5) "nginx"
'date' =>
string(29) "Sat, 27 Feb 2016 02:36:49 GMT"
'content-type' =>
string(31) "application/json; charset=UTF-8"
'transfer-encoding' =>
string(7) "chunked"
'connection' =>
string(10) "keep-alive"
'x-asana-content-string-length' =>
string(3) "219"
'pragma' =>
string(8) "no-cache"
'set-cookie' =>
string(22) "TooBusyRedirectCount=0"
'cache-control' =>
string(8) "no-store"
'x-asana-preferred-release-revision' =>
string(56) "20160226_220201_ce3e88b3589d50e067532bbd0e824a44148e6738"
'x-robots-tag' =>
string(4) "none"
}
}
public $raw_headers =>
string(404) "HTTP/1.1 400 Bad Request
Server: nginx
Date: Sat, 27 Feb 2016 02:36:49 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Asana-Content-String-Length: 219
Pragma: no-cache
Set-Cookie: TooBusyRedirectCount=0
Cache-Control: no-store
X-Asana-Preferred-Release-Revision: 20160226_220201_ce3e88b3589d50e067532bbd0e824a44148e6738
X-Robots-Tag: none"
public $request =>
class Httpful\Request#962 (22) {
public $uri =>
string(62) "https://app.asana.com/api/1.0/tasks/(task_id)/attachments"
public $method =>
string(4) "POST"
public $headers =>
array(2) {
'X-Asana-Client-Lib' =>
string(118) "version=0.4.0&language=PHP&language_version=5.6.18-1%2Bdeb.sury.org%7Etrusty%2B1&os=Linux&os_version=3.19.0-25-generic"
'Authorization' =>
string(41) "Bearer (valid bearer)"
}
public $raw_headers =>
string(487) "POST /api/1.0/tasks/(task_id)/attachments HTTP/1.1
Host: app.asana.com
Expect:
User-Agent: Httpful/0.2.20 (cURL/7.35.0 PHP/5.6.18-1+deb.sury.org~trusty+1 (Linux))
Content-Type: multipart/form-data
Accept: */*; q=0.5, text/plain; q=0.8, text/html;level=3;q=0.9, application/json
X-Asana-Client-Lib: version=0.4.0&language=PHP&language_version=5.6.18-1%2Bdeb.sury.org%7Etrusty%2B1&os=Linux&os_version=3.19.0-25-generic
Authorization: Bearer 0/88eebe64d4af7bd966936bbe346be679
"
public $strict_ssl =>
bool(false)
public $content_type =>
string(19) "multipart/form-data"
public $expected_type =>
string(16) "application/json"
public $additional_curl_opts =>
array(0) {
}
public $auto_parse =>
bool(true)
public $serialize_payload_method =>
int(0)
public $username =>
NULL
public $password =>
NULL
public $serialized_payload =>
array(1) {
'file' =>
string(48) "#/tmp/YCRYJd;filename=upload.txt;type=text/plain"
}
public $payload =>
array(1) {
'file' =>
string(48) "#/tmp/YCRYJd;filename=upload.txt;type=text/plain"
}
public $parse_callback =>
NULL
public $error_callback =>
NULL
public $send_callback =>
NULL
public $follow_redirects =>
bool(false)
public $max_redirects =>
int(25)
public $payload_serializers =>
array(0) {
}
public $_ch =>
resource(652) of type (Unknown)
public $_debug =>
NULL
}
public $code =>
int(400)
public $content_type =>
string(16) "application/json"
public $parent_type =>
string(16) "application/json"
public $charset =>
string(5) "UTF-8"
public $meta_data =>
array(26) {
'url' =>
string(62) "https://app.asana.com/api/1.0/tasks/(task_id)/attachments"
'content_type' =>
string(31) "application/json; charset=UTF-8"
'http_code' =>
int(400)
'header_size' =>
int(408)
'request_size' =>
int(552)
'filetime' =>
int(-1)
'ssl_verify_result' =>
int(0)
'redirect_count' =>
int(0)
'total_time' =>
double(3.435421)
'namelookup_time' =>
double(2.1E-5)
'connect_time' =>
double(0.29363)
'pretransfer_time' =>
double(2.023479)
'size_upload' =>
double(187)
'size_download' =>
double(219)
'speed_download' =>
double(63)
'speed_upload' =>
double(54)
'download_content_length' =>
double(-1)
'upload_content_length' =>
double(187)
'starttransfer_time' =>
double(2.023485)
'redirect_time' =>
double(0)
'redirect_url' =>
string(0) ""
'primary_ip' =>
string(12) "52.70.61.144"
'certinfo' =>
array(0) {
}
'primary_port' =>
int(443)
'local_ip' =>
string(9) "10.0.2.15"
'local_port' =>
int(49537)
}
public $is_mime_vendor_specific =>
bool(false)
public $is_mime_personal =>
bool(false)
private $parsers =>
NULL
}
[Asana\Errors\InvalidRequestError]
Invalid Request
Looked into it and Asana/php-asana uploadFile code is not compatible with php 5.6+.
Pull request: here

PHP cannot find file for require_once, but it's there?

It's an error in MyBB, after install I get this error from the index:
Warning: require_once(./global.php): failed to open stream: No such file or directory in C:\jws\htdocs\index.php on line 18
Fatal error: require_once(): Failed opening required './global.php' (include_path='.;C:\php\pear') in C:\jws\htdocs\index.php on line 18
Index code near top:
require_once './global.php';
require_once MYBB_ROOT.'inc/functions_forumlist.php';
require_once MYBB_ROOT.'inc/class_parser.php';
The file is there, I checked in the folder. I haven't changed anything in the files. I'm using PHP 5.6.4, and the index file is loaded from a require_once in another file.
Here:
<?php
$_SERVER = array(
"SERVER_PROTOCOL" => "HTTP/1.1","DOCUMENT_ROOT" => "C:/jws/htdocs","REMOTE_ADDR" => "127.0.0.1","REQUEST_URI" => "/","SERVER_SOFTWARE" => "JWS/1.0","argv" => "","REQUEST_TIME" => "Tue, 30 Dec 2014 12:33:14 PST","PHP_SELF" => "/","REMOTE_PORT" => "56601","REQUEST_METHOD" => "GET","SCRIPT_NAME" => "","SERVER_NAME" => "JWS/1.0","PATH_INFO" => "","ORIG_PATH_INFO" => "","argc" => "","GATEWAY_INTERFACE" => "N/I","SERVER_PORT" => "80","SCRIPT_FILENAME" => "C:/jws/htdocs/index.php","HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.5","HTTP_HOST" => "127.0.0.1","QUERY_STRING" => "","REQUEST_TIME_FLOAT" => "1419971594194","HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0","HTTP_ACCEPT_ENCODING" => "gzip, deflate","SERVER_ADDR" => "127.0.0.1","HTTP_CONNECTION" => "keep-alive",
);
$_GET = array(
);
$_POST = array(
);
$_COOKIE = array(
);
$_REQUEST = array(
);
require_once 'C:/jws/htdocs/index.php';
?>
replace your index code with this
require_once 'global.php';
require_once MYBB_ROOT.'inc/functions_forumlist.php';
require_once MYBB_ROOT.'inc/class_parser.php';
I managed to use:
chdir("the path");
to change the working directory, and I also had issues with redirection failure in the server.
Thanks for the help!
index.php code near top:
<?PHP
require_once MYBB_ROOT.'/global.php';
require_once MYBB_ROOT.'/inc/functions_forumlist.php';
require_once MYBB_ROOT.'/inc/class_parser.php';
?>
indexTest.php
<?php
define("MYBB_ROOT", __DIR__);
$_SERVER = array(
"SERVER_PROTOCOL" => "HTTP/1.1","DOCUMENT_ROOT" => "C:/jws/htdocs","REMOTE_ADDR" => "127.0.0.1","REQUEST_URI" => "/","SERVER_SOFTWARE" => "JWS/1.0","argv" => "","REQUEST_TIME" => "Tue, 30 Dec 2014 12:33:14 PST","PHP_SELF" => "/","REMOTE_PORT" => "56601","REQUEST_METHOD" => "GET","SCRIPT_NAME" => "","SERVER_NAME" => "JWS/1.0","PATH_INFO" => "","ORIG_PATH_INFO" => "","argc" => "","GATEWAY_INTERFACE" => "N/I","SERVER_PORT" => "80","SCRIPT_FILENAME" => "C:/jws/htdocs/index.php","HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.5","HTTP_HOST" => "127.0.0.1","QUERY_STRING" => "","REQUEST_TIME_FLOAT" => "1419971594194","HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0","HTTP_ACCEPT_ENCODING" => "gzip, deflate","SERVER_ADDR" => "127.0.0.1","HTTP_CONNECTION" => "keep-alive",
);
$_GET = array(
);
$_POST = array(
);
$_COOKIE = array(
);
$_REQUEST = array(
);
require_once MYBB_ROOT.'/index.php';
?>

Categories