├── README.md
└── index.php
/README.md:
--------------------------------------------------------------------------------
1 | # Simple View Counter
2 |
3 |
4 |
5 |
6 |
7 |
8 | Simple view counter for websites and Github READMEs.
9 |
10 | ## Demo
11 |
12 |
13 |
14 | ## Watch me make this
15 |
16 | https://youtu.be/oDQCyQahn68
17 |
18 |
19 |
20 |
21 | ## Deploy
22 |
23 | Want this for your repository? Simply upload the [`index.php`](/index.php) to any web server with PHP installed.
24 |
25 | > **Note**
26 | > You may need to manually create the `views.txt` file adjacent to the `index.php` and enable write permissions for it depending on your server configuration.
27 |
28 | > **Warning**
29 | > Heroku is not recommended for hosting this project since it has an ephemeral file system and will remove the `views.txt` when the source code is refreshed.
30 |
31 | ## Support
32 |
33 | 💖 If you like this project, give it a ⭐ and share it with friends!
34 |
35 |
36 |
37 |
38 |
39 |
40 | ☕ Buy me a coffee
41 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | = 1000; $i++) {
50 | $num /= 1000;
51 | }
52 | return round($num, 1) . $units[$i];
53 | }
54 |
55 | // get contents of a URL with curl
56 | function curl_get_contents($url): string
57 | {
58 | $ch = curl_init();
59 | curl_setopt($ch, CURLOPT_HEADER, 0);
60 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
61 | curl_setopt($ch, CURLOPT_URL, $url);
62 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
63 | curl_setopt($ch, CURLOPT_VERBOSE, 0);
64 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
65 | $response = curl_exec($ch);
66 | curl_close($ch);
67 | return $response;
68 | }
69 |
70 | // increment the file and get the current count
71 | $message = incrementFile("views.txt");
72 |
73 | // set parameters for the shields.io URL
74 | $params = [
75 | "label" => "Views",
76 | "logo" => "github",
77 | "message" => shortNumber($message),
78 | "color" => "purple",
79 | "style" => "for-the-badge"
80 | ];
81 |
82 | // build the URL with an SVG image of the view counter
83 | $url = "https://img.shields.io/static/v1?" . http_build_query($params);
84 |
85 | // output the response (svg image)
86 | echo curl_get_contents($url);
87 |
--------------------------------------------------------------------------------