Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPの様々なエラー
Search
uzulla
March 16, 2019
Programming
0
180
PHPの様々なエラー
2019/03/17 at hachioji.pm #75 LT
uzulla
March 16, 2019
Tweet
Share
More Decks by uzulla
See All by uzulla
似たもの同士のPerlとPHP
uzulla
1
130
More Context, Better Code. 既存コードやOAS等をコンテキストとしてLLMに与える事で、よりよいコード生成を行う話
uzulla
1
110
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
3
180
セッションのトークセッション / Traps for PHP session features in growing web apps
uzulla
2
120
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
5
2k
例外を投げるのをやめてみないか? あるいは受け入れてみないか? - How to use exceptions other than throwing
uzulla
4
940
PHPerが ISUCONでやるべき事
uzulla
1
1k
開発生産性は上がらない - N Ways to Reduce Development Productivity
uzulla
1
240
test is not a job
uzulla
1
500
Other Decks in Programming
See All in Programming
Haze - Real time background blurring
chrisbanes
1
510
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
450
선언형 UI에서의 상태관리
l2hyunwoo
0
140
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
モバイルアプリにおける自動テストの導入戦略
ostk0069
0
110
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
Full stack testing :: basic to basic
up1
1
930
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
700
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
95
5.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Six Lessons from altMBA
skipperchong
27
3.5k
Typedesign – Prime Four
hannesfritz
40
2.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Designing Experiences People Love
moore
138
23k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Why Our Code Smells
bkeepers
PRO
335
57k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Transcript
PHPͷ༷ʑͳΤϥʔ hachiojipm #75 2019/03/16 @uzulla
લఏ » ඞͣ͠·͠ΐ͏ɻ error_reporting(E_ALL);
PHPͷΤϥʔ » ࣗ༝ͳՕॴͰͷΩϟον͔ͳΓࠔ͕ͩɺϋϯυϥΛઃఆͰ͖Δ » PHP 7ʹͳΓɺେମ͕ʮErrorʯͱ͍͏Exceptionʹͳͬͨ » Ωϟον͠ͳ͍ͱ(ຊͷ)Errorʹม͞ΕΔ » ʢঘɺྫ֎Ωϟον͠ͳ͚Ε͍ͣΕʹͤΑࢮ͵ʣ
» શ෦͕ྫ֎Ͱͳ͍͕ɺେମThrowableΛΩϟον͢ΕΑ͍ » ͔͠͠Թނ৽ɺൿͷλϨΛ͔͖ճͧ͢ɻ
Τϥʔͷେ·͔ͳྨʢཁग़యʣ » ແ༻Ͱࢮ͵ͭ » Fatal Errors » Parse Error »
ࢮ͵͚Ͳख͢Εࢮͳͳ͍ͭ » Recoverable Fatal Error » ख͠ͳͯ͘ࢮͳͳ͍ͭ » Warning » Noticeʢ
recoverable ͱ… » Τϥʔʢྫ֎Έ͍ͨͳͷʣΛΩϟονͯ͠ѲΓͭͿͤࢮͳͳ͍ » ޙड़
ྨΛ͞Βʹࡉʹ
Fatal, Parse(Syntax) Error » E_ERROR » E_CORE_ERROR » E_COMPILE_ERROR »
E_USER_ERROR » E_PARSE
Recoverable Fatal Error » E_RECOVERABLE_ERROR
Warning » E_WARNING » E_CORE_WARNING » E_COMPILE_WARNING » E_USER_WARNING
Notice ͳͲ » E_DEPRECATED » E_USER_DEPRECATED » E_NOTICE » E_USER_NOTICE
» (E_STRICT)
E_ERROR (1<<0L) 1 E_WARNING (1<<1L) 2 E_PARSE (1<<2L) 4 E_NOTICE
(1<<3L) 8 E_CORE_ERROR (1<<4L) 16 E_CORE_WARNING (1<<5L) 32 E_COMPILE_ERROR (1<<6L) 64 E_COMPILE_WARNING (1<<7L) 128 E_USER_ERROR (1<<8L) 256 E_USER_WARNING (1<<9L) 512 E_USER_NOTICE (1<<10L) 1024 E_STRICT (1<<11L) 2048 E_RECOVERABLE_ERROR (1<<12L) 4096 E_DEPRECATED (1<<13L) 8192 E_USER_DEPRECATED (1<<14L) 16384
ͰҾ͖ى͜͠·͠ΐ͏
E_ERROR » DateTimeInterfaceΛΠϯϓϦ͠Α͏ͱ͢ΔͳͲ class b implements DateTimeInterface { } new
b();
ଈࢮ͢ΔͷͰ » ʢޙड़ͷʣset_error_handerݺΕͳ͍ » ͳʹͰ͖ͳ͍ɺඞͣࢮ͵ » ͔͠͠ɺΤϥʔͷऔΓ͘Β͍Ͱ͖Δ
Δʹ register_shutdown_function(function () { $error = error_get_last(); if ( !is_array($error)
|| !( $error['type'] & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_PARSE | E_USER_ERROR) ) ) { return; // set_error_handelerͰर͍͑ͯΔͣͳͷͰແࢹ } // र͑ͳ͔ͬͨFATALͳΤϥʔ echo "{$error['type']}:{$error['message']}". " in {$error['file']}:{$error['line']}"; });
۩ମྫ <?php register_shutdown_function(function () { $error = error_get_last(); if (
!is_array($error) || !( $error['type'] & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_PARSE | E_USER_ERROR) ) ) { return; } error_log("Fatal Error:{$error['type']}:{$error['message']}". " in {$error['file']}:{$error['line']}"); }); class b implements DateTimeInterface {} new b();
» Կݴ͏͚Ͳɺٹ͑ΔΘ͚Ͱͳ͍ » ۤͷϩάͨ͠ΓɺΤϥʔը໘Λग़ͨ͠Γఔ » ૹ৴ͨ͠σʔλ͕ͲΔΘ͚Ͱͳ͍ͷͰɺΰϛͷͳ͍ΩϨΠͳΤ ϥʔը໘Λग़ͨ͢Ίʹग़ྗͳͲΛOutput bufferingͯ͠ૹ৴͠ͳ ͍Ͱ͓͘͜ͱ »
https://uzulla.hateblo.jp/entry/2019/03/13/200820 » ͋·Γڽͬͨࣄ͠ͳ͍΄͏͕ແʢ͢Ͱʹ͓͔͍͠ঢ়ଶ͔ͩΒ Ͷʂʣ
E_CORE_ERROR » vmपΓͷΤϥʔ…ൃੜͤ͞Δํ๏͕Θ͔ΒΜ » odbcʹ͋Γͦ͏ʢ͔͠͠odbcΛ͏ࣄͳͲ͋Δͷ͔…ʁ » (ྑ͍αϯϓϧίʔυͭ͘Εͳ͔ͬͨ!)
E_COMPILE_ERROR » ఆٛࡁΈͷΫϥεΛએݴ͢ΕΑ͍ class DateTime{}
E_USER_ERROR » ࣗͰ͛ΒΕΔ » trigger_error trigger_error("test error", E_USER_ERROR);
E_PARSE » จ๏ؒҧ͑ΔͳͲ » ࢮ͵͠ɺ࠷ॳͷϑΝΠϧͩͱregister_shutdown_functionݺΕͳ͍ <?php require("bad.php"); // register_shutdown_functionݺΕΔ ...
<?php if if (1){} // ͳʹͰ͖ͣଈࢮ
ҙɺEvalʹ͍ͭͯ » evalE_PARSE͕ൃੜ͢Δͷ͕ͩɺྫ֎ͷΩϟονΛ͠ͳ͍߹ͷΈੜ·ΕΔ » ͳ͓ɺྫ֎ΛΩϟον͠ͳ͍ͱFatal Errorѻ͍ʹͳΓɺࢮ͵ɻ » PHPϜζΧγΠωʔ // ࢮͳͳ͍ʢ͠ɺѲΓ௵ͤΤϥʔͳ͍ʣ
try{ eval("if if(1){};"); }catch(\Throwable $e){ } // Uncaught ExceptionͰ͝ͱࢮ͵ eval("if if(1){};");
E_RECOVERABLE_ERROR » ΦϒδΣΫτΛແͳΩϟετ͢ΔͳͲͯ͠ൃੜͰ͖Δ » ࢮ͵…͕ɺ෮׆Ͱ͖Δ echo function() {};
ͨͱ͑͜ΕΛೖΕΔͱɺऴྃ͠ͳ͍ » NoticeؚΊɺ͋ΒΏΔΤϥʔʢFatalআ͘ʣ͕ඈͼࠐΉ » Notice͘ΔͷͰɺ͜͜ͰExit͢ΔͱNoticeڐ͞ͳ͍ࣄ͕Ͱ͖Δ ʢ෭࡞༻తϝϦοτʢओ؍ʣʣ set_error_handler(function ($type, $message, $file,
$line) { error_log("Error: {$type}:{$message} in {$file}:{$line}"); // ͜͜Ͱ exit͠ͳ͍ͱɺ࣮ͬͯߦ͕ଓ͘ // ྫ֎ͷѲΓͭͿ͠ͷΑ͏ͳͷ });
E_WARNING » ͢ͰʹϘσΟ͕ૹ৴։࢝͞Ε͍ͯΔͷʹɺϔομʔΛ͓͘Ζ͏ͱͨ͠ ࣌ͳͲ » ࢮͳͳ͍ɺͷ͕ࠔΔɻʢͷͰɺલड़ͷςΫΛ͏ͱΑ͍ࣄʣ header("X-MY-HEADER: hoge"); echo "hoge";
session_start();
E_CORE_WARNING » ଘࡏ͠ͳ͍֦ுΛphp.iniͰࢦఆ͢Δ » Γαϯϓϧίʔυ͕͍͍ײ͡ͷ͕ͳ͔ͬͨͷͰলུ
E_COMPILE_WARNING » /*Λด͡ͳ͍ͳͲ » ιʔείʔυͷจࣈίʔυΛ͓͔͘͢͠ΔͳͲ » ͜ΕɺWarnͱ͍͏͔Syntax ErrorͰͳ͍ͷ͔ͱ… <php /*
E_USER_WARNING » ࣗͰtrigger͢Δ trigger_error("test error", E_USER_WARNING);
E_DEPRECATED » ඇਪͷؔʢڍಈʣΛͤ͞Δ » error_reporting(E_ALL)ͯ͠ͳ͍ͱͰͳ͍ error_reporting(E_ALL); define("SomeThing", "is", true);
E_USER_DEPRECATED » ࣗͰ͛Δ » error_reporting(E_ALL)ͯ͠ͳ͍ͱͰͳ͍ trigger_error("test error", E_USER_DEPRECATED);
E_NOTICE » ະఆٛมʹ৮Δɺଘࡏ͠ͳ͍ൣғͷྻʹ৮Δ » error_reporting(E_ALL)ͯ͠ͳ͍ͱͰͳ͍ echo $undefined;
E_USER_NOTICE » ࣗͰ͛Δ » error_reporting(E_ALL)ͯ͠ͳ͍ͱͰͳ͍ trigger_error("test error", E_USER_DEPRECATED);
(E_STRICT) » PHP5·Ͱ͔͠ͳ͍ɺ7Ͱͳ͘ͳͬͨ(ଞʹҠಈ͞Εͨ) » ܧঝͨ͠ΫϥεϝιουͷϑΟϯΨʔϓϦϯτ͕ҧ͏ͱ͔ʢҾ͕ม ΘΔͱ͔ʣ
·ͱΊ » Ή͔͍ͣ͠ » ൿͷλϨ͕Δʢόʔδϣϯ্͕͕Δͱඍົʹʣ » ωοτʹʢࣗΛؚΊͯʣӕ͕ଟ͍ͷͰɺݕূͨ͠Γɺͨ·ʹλϨ Λ͔͖·ͥͳ͍ͱ͍͚ͳ͍ʢࠓ͔͖ࠞͥͨΒؒҧͬͯͨ…ʣ » ϏοτͷฒͼΛͳ͕Ί͍ͯΔͱɺ૿ઃͷྺ࢙Λײ͡ΒΕΔ
» ·͋PHPͳͷͰͶʂ
༨ஊ@ʹ͍ͭͯ » @Λ͚ͭͨࣜͷΤϥʔΛʮग़ྗ͠ͳ͍ʯʢ͚ͩʣ // $aม͕ແ͍࣌ php > echo @$a; //
ΤϥʔͰͳ͍ɺNULLʹͳΔ php > echo $a; PHP Notice: Undefined variable: a in php shell code on line 1
» @ͳΒFatalͰࢮͳͳ͍Θ͚Ͱͳ͘ʢࠂͳ͘ࢮ͵͕ʣ » @ݟ͚ͭͨΒʢཧ༝͕ͳ͚Εʣফ͠·͠ΐ͏ // ݹɺ͜͏͍͏ॻ͖ํ͋ͬͨͳ $id = @$_POST['id']; //
͍·ͳΒ͜͏͔ $id = $_POST['id'] ?? null; » ͳ͓ɺࣗલͰset_error_handlerͨ͠Βແࢹ͞Εͳ͍ͷͰɺ@׆༻͢ Δݹ͍ϥΠϒϥϦΛ͏࣌ҙʂ