{"id":23936650,"url":"https://github.com/tichavich/csrf-token-php","last_synced_at":"2026-06-13T06:31:46.366Z","repository":{"id":232262562,"uuid":"783882272","full_name":"tichavich/CSRF-Token-PHP","owner":"tichavich","description":"cross-site request forgery (CSRF)","archived":false,"fork":false,"pushed_at":"2024-04-10T20:06:15.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-24T02:50:55.903Z","etag":null,"topics":["csrf","csrf-tokens","php","php-sessions","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tichavich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-08T19:01:31.000Z","updated_at":"2024-06-03T16:28:52.000Z","dependencies_parsed_at":"2024-04-10T20:30:47.281Z","dependency_job_id":null,"html_url":"https://github.com/tichavich/CSRF-Token-PHP","commit_stats":null,"previous_names":["tichavich/csrf-token-php"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tichavich/CSRF-Token-PHP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tichavich%2FCSRF-Token-PHP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tichavich%2FCSRF-Token-PHP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tichavich%2FCSRF-Token-PHP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tichavich%2FCSRF-Token-PHP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tichavich","download_url":"https://codeload.github.com/tichavich/CSRF-Token-PHP/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tichavich%2FCSRF-Token-PHP/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34275065,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-13T02:00:06.617Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csrf","csrf-tokens","php","php-sessions","php8"],"created_at":"2025-01-06T01:15:13.160Z","updated_at":"2026-06-13T06:31:46.349Z","avatar_url":"https://github.com/tichavich.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSRF-Token-PHP\ncross-site request forgery (CSRF)\n\n## Table of content\n\n1. [CSRF คืออะไร](#CSRF)\n2. [ฟังก์ชั่นที่สำคัญ](#function)\n3. [วิธีการใช้งาน](#manual-guide)\n\n\n# CSRF คืออะไร ?\nCSRF ย่อมาจาก Cross-Site Request Forgery เป็นช่องโหว่ที่เกิดขึ้นเมื่อผู้ไม่ประสงค์ดีส่งคำขอ (request) จากเว็บไซต์อื่น (ที่ไม่ได้รับอนุญาต) ไปยังเว็บไซต์ที่ผู้ใช้งานได้เข้าถึงอยู่ โดยใช้ความเชื่อมต่อที่ผู้ใช้งานสามารถเข้าถึงได้เพื่อกระทำการที่ไม่พึงประสงค์ เช่น การเปลี่ยนแปลงข้อมูลส่วนตัว การเพิ่มข้อมูล หรือการกระทำคำสั่งอื่นๆ ที่สามารถทำให้เกิดผลกระทบในระบบของผู้ใช้งานหรือเว็บไซต์นั้นๆ\n\n# ฟังก์ชั่นที่สำคัญ\n- ฟังก์ชัน hash_equals ใช้สำหรับเปรียบเทียบสตริงของค่าแฮชแบบแข็งแรง (hash) แบบคงเดิม (constant-time comparison) โดยไม่สนใจความยาวของสตริง และมีการป้องกันการโจมตีแบบแฝงข้อความ (timing attacks) ซึ่งเป็นวิธีการโจมตีที่ใช้เวลาเพิ่มเติมในการประมวลผลเมื่อเปรียบเทียบสตริง\n- ฟังก์ชัน bin2hex ใช้สำหรับแปลงข้อมูลในรูปแบบ binary เป็นข้อความฐานสิบหก (hexadecimal) ซึ่งมักจะใช้ในการเข้ารหัสข้อมูลเช่นในการเข้ารหัสข้อมูลไบนารีเป็นข้อความที่อ่านได้ หรือการเข้ารหัสข้อมูลในรูปแบบที่ไม่มีอักขระพิเศษใน URL ด้วยเอง เป็นต้น\n- ฟังก์ชัน random_bytes(): ใช้สร้างข้อมูลสุ่มโดยใช้เครื่องมือสุ่มในระบบปฏิบัติการ (operating system's CSPRNG - Cryptographically Secure Pseudo-Random Number Generator) ที่มีความปลอดภัยสูง ข้อมูลที่สร้างขึ้นมาจะเป็นข้อมูลที่สุ่มจริงที่มีความเสมือนจริงและปลอดภัยในการใช้งาน.\n\n# วิธีใช้งาน\n- Client\n  \u003e แบบที่ 1 สร้าง token key แปะไว้ในส่วน Head\n    ```\n    \u003c?php\n      require_once('CSRF.php');\n      CSRF::generateToken();  //สร้าง Tokey-key\n      CSRF::metaContent();  //เพิ่ม meta csrf_token in head HTML\n    ?\u003e\n    ```\n    Result :\n    ```\n    \u003c!DOCTYPE html\u003e\n    \u003chtml lang=\"en\"\u003e\n    \u003chead\u003e\n        \u003cmeta charset=\"UTF-8\"\u003e\n        \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n        \u003ctitle\u003e..\u003c/title\u003e\n        \u003cmeta name=\"csrf_token\" content=\"4f478e462bcb5d507875d4dea663bdababf614292797206a9ccfcd9881abde568d4d04\"\u003e  \n    \u003c/head\u003e\n    \u003cbody\u003e\u003c/body\u003e\n    \u003c/html\u003e\n    ```\n  \u003e แบบที่ 2 สร้าง token key เก็บลง input hidden\n    ```\n    \u003c?php\n      require_once('CSRF.php');\n      CSRF::generateToken();  //สร้าง Tokey-key\n      CSRF::inputForm();  //เพิ่ม input form\n    ?\u003e\n    ```\n    Result :\n    ```\n    \u003cform method=\"POST\"\u003e\n    \u003cinput type=\"hidden\" name=\"csrf_token\" value=\"4f478e462bcb5d507875d4dea663bdababf614292797206a9ccfcd9881abde568d4d04\"\u003e\n    \u003c/form\u003e\n    ```\n\n- Server\n    \u003e ส่ง token key มาที่ Server\n    ```\n    $.ajax({\n      headers: {\n          'X-CSRF-TOKEN': document.querySelector(`meta[name=\"csrf_token\"]`).getAttribute('content')\n      },\n        url: \"...\",\n        method: \"GET\",\n        data: {id:453}\n    };\n    ```\n   \u003e ตรวจสอบ token key\n    ```\n    \u003c?php\n      require_once('CSRF.php');\n      if(CSRF::verificationToken()){\n        ...\n      }\n    ?\u003e\n    ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftichavich%2Fcsrf-token-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftichavich%2Fcsrf-token-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftichavich%2Fcsrf-token-php/lists"}