tag:blogger.com,1999:blog-78306577599186353362024-03-02T09:30:12.513-08:00Code SiriShare programming knowledgeUnknownnoreply@blogger.comBlogger57125tag:blogger.com,1999:blog-7830657759918635336.post-32740855807512135232023-12-05T19:05:00.000-08:002023-12-05T19:05:40.035-08:00Edit, add, or remove ports of an existing Docker container<p>In some cases you need to assign additional ports to a running Docker container, or change the ports in use. This is completely possible without creating a new docker image or re-running docker run to create a new container. To change the ports of a running Docker Container, follow the instructions below.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHZVrAWQyM8DHgwloX5A-h-2Xo8GvL0VXzl44yCQpfrU3JtFsLr_5ssBXM3q81z4bnb1RmapCDrKLHOlVYtt1ImJzZUGomT0zkQmOTfWOFy3Sn_mnRLkyE7nxHIfnzMORWITMB0a31gG8mbcTXBWFInq_N-ssuEolqK2Wg7YYYPVE1ZelMSg8-E2sHgMA/s1344/Docker.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="896" data-original-width="1344" height="151" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHZVrAWQyM8DHgwloX5A-h-2Xo8GvL0VXzl44yCQpfrU3JtFsLr_5ssBXM3q81z4bnb1RmapCDrKLHOlVYtt1ImJzZUGomT0zkQmOTfWOFy3Sn_mnRLkyE7nxHIfnzMORWITMB0a31gG8mbcTXBWFInq_N-ssuEolqK2Wg7YYYPVE1ZelMSg8-E2sHgMA/w227-h151/Docker.png" width="227" /></a></div><p><b>Example:</b> we have a container of image php:8.1-apache configured with port mapping 8080->80/tcp. We will edit this container to open port 8081->443/tcp.</p><p><i><span style="color: #cc0000;"><b>Warning!</b> You should back up before editing any files</span></i></p><p><span style="font-size: large;"><b>Step 1:</b> Go to your container configuration directory</span></p><p>Go to the directory where the containers are stored</p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">cd /var/lib/docker/containers</span></blockquote><p></p><p>Here you will see folders with names corresponding to container ids. Then access the directory of the corresponding container you need to edit.</p><p>The full path will be as follows</p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">/var/lib/docker/containers/your_container_id_hash</span></blockquote><p></p><p>You can find the container's hash id via the command</p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">docker ps -a</span></blockquote><p></p><p><span style="font-size: large;"><b>Step 2:</b> Stop docker service (docker.socket)</span></p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">systemctl stop docker.socket</span></blockquote><p></p><p><span style="font-size: large;"><b>Step 3:</b> Edit expose ports config in file <span style="color: #990000;"><i>config.v2.json</i></span></span></p><p>We will declare port 443 on the container</p><p>Find json string </p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">"ExposedPorts":{"80/tcp":{}}</span></blockquote><p></p><p>Then add new port</p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">"ExposedPorts":{"80/tcp":{},"443/tcp":{}}</span></blockquote><p></p><p><span style="font-size: large;"><b>Step 4:</b> Edit ports mapping config in file <i><span style="color: #990000;">hostconfig.json</span></i></span></p><p>We will map port 8081 on the host computer to the newly created port 443/tcp on the container</p><p>Find json string </p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"8080"}]}</span></blockquote><p></p><p>Then add new port</p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"8081"}],"80/tcp":[{"HostIp":"","HostPort":"8080"}]}</span></blockquote><p></p><p><span style="font-size: large;"><b>Step 5:</b> Restart the docker service</span></p><p><span style="color: #cc0000;"></span></p><blockquote><span style="color: #cc0000;">systemctl start docker</span></blockquote><p></p><p><span style="font-size: large;"><b>Done !</b></span></p><div id="gtx-trans" style="left: 68px; position: absolute; top: 285.818px;"><div class="gtx-trans-icon"></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-52582478224806625932023-02-12T02:16:00.003-08:002023-02-17T08:18:11.143-08:00PHP - Access ChatGPT API using cURL<p>To access OpenAI's ChatGPT API using PHP, you can use any HTTP client library that supports making HTTP requests with JSON payloads, such as Guzzle or cURL. You may find it a bit ridiculous, because ChatGPT hasn't officially announced the API yet. But the fact that OpenAI already has APIs available to provide full task processing like ChatGPT. This REST API can answer your questions or handle your requests with the same results as ChatGPT.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKnK0_UgwJDSEVA95INOlK4nmp2HyqTlmaFVCDqml18dWPZ8u02czwkh1_BfqVaaUtiXJPLOCSYAxSI9v_5rgIV2Z5IEGYU5JhIw2ZN7_SCc-hEHN2ZcpYKRmRlb-MwdFwG_pUMhwSoW55rV0iJs5_nXbYxbeQRDBKQzbXJei58K8C4UuZoTkye-1n/s682/ChatGPT%20API.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="400" data-original-width="682" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKnK0_UgwJDSEVA95INOlK4nmp2HyqTlmaFVCDqml18dWPZ8u02czwkh1_BfqVaaUtiXJPLOCSYAxSI9v_5rgIV2Z5IEGYU5JhIw2ZN7_SCc-hEHN2ZcpYKRmRlb-MwdFwG_pUMhwSoW55rV0iJs5_nXbYxbeQRDBKQzbXJei58K8C4UuZoTkye-1n/s320/ChatGPT%20API.jpg" width="320" /></a></div><h2>How GPT Chat Works</h2><p>As a general-purpose language model, ChatGPT is likely to use a combination of OpenAI's available models and techniques to generate its responses, depending on the context and nature of the question. Additionally, OpenAI may update or modify its models over time, so the specific models used by ChatGPT may change as well. In other words, if you use the OpenAI API with the right Model, you can get the same results as ChatGPT. Of course ChatGPT will have more features and especially the ability to link topics with your messages.</p><h2>How to access OpenAI ChatGPT API using Curl</h2><p>You can study the documents and examples at the following links:</p><div>OpenAI Docs: <span style="color: #2b00fe;"><i>https://platform.openai.com/docs/introduction</i></span><br />Models: <i><span style="color: #2b00fe;">https://platform.openai.com/docs/models/gpt-3</span></i><br />Features and examples: <i><span style="color: #2b00fe;">https://platform.openai.com/examples</span></i><br />Get API Key: <i><span style="color: #2b00fe;">https://platform.openai.com/account/api-keys</span></i></div><p><b><span style="font-size: medium;">ChatGPT API Sample:</span></b></p><div style="background-color: white; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: maroon;"><?php</span></div><div><span style="color: #001080;">$data</span> = <span style="color: #795e26;">array</span>(</div><div> <span style="color: #a31515;">"prompt"</span> => <span style="color: #a31515;">"Is Climate Change worrisome?"</span>, //Your question or request</div><div> <span style="color: #a31515;">"temperature"</span> => <span style="color: #098658;">0.5</span>,</div><div> <span style="color: #a31515;">"max_tokens"</span> => <span style="color: #098658;">500</span></div><div>);</div><br /><div><span style="color: #001080;">$ch</span> = <span style="color: #795e26;">curl_init</span>();</div><div><span style="color: #795e26;">curl_setopt</span>(<span style="color: #001080;">$ch</span>, CURLOPT_URL, <span style="color: #a31515;">'https://api.openai.com/v1/engines/davinci-codex/completions'</span>);</div><div><span style="color: #795e26;">curl_setopt</span>(<span style="color: #001080;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #098658;">1</span>);</div><div><span style="color: #795e26;">curl_setopt</span>(<span style="color: #001080;">$ch</span>, CURLOPT_POST, <span style="color: #098658;">1</span>);</div><div><span style="color: #795e26;">curl_setopt</span>(<span style="color: #001080;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: #795e26;">json_encode</span>(<span style="color: #001080;">$data</span>));</div><br /><div><span style="color: #001080;">$headers</span> = <span style="color: #795e26;">array</span>();</div><div><span style="color: #001080;">$headers</span>[] = <span style="color: #a31515;">'Content-Type: application/json'</span>;</div><div><span style="color: #001080;">$headers</span>[] = <span style="color: #a31515;">'Authorization: Bearer YOUR_API_KEY'</span>;</div><div><span style="color: #795e26;">curl_setopt</span>(<span style="color: #001080;">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: #001080;">$headers</span>);</div><br /><div><span style="color: #001080;">$response</span> = <span style="color: #795e26;">curl_exec</span>(<span style="color: #001080;">$ch</span>);</div><div><span style="color: #af00db;">if</span> (<span style="color: #795e26;">curl_errno</span>(<span style="color: #001080;">$ch</span>)) {</div><div> <span style="color: #795e26;">echo</span> <span style="color: #a31515;">'Error:'</span> . <span style="color: #795e26;">curl_error</span>(<span style="color: #001080;">$ch</span>);</div><div>}</div><div><span style="color: #795e26;">curl_close</span>(<span style="color: #001080;">$ch</span>);</div><div><span style="color: #795e26;">echo</span> <span style="color: #001080;">$response</span>;</div><div><span style="color: green;">/**</span></div><div><span style="color: green;"> * Output Response :</span></div><div><span style="color: green;"> * Yes, climate change is dangerous and has significant consequences </span></div><div><span style="color: green;"> * for the planet and its inhabitants. The warming of the Earth's climate </span></div><div><span style="color: green;"> * due to increased greenhouse gas emissions, primarily from human </span></div><div><span style="color: green;"> * activities such as burning fossil fuels, is causing more frequent </span></div><div><span style="color: green;"> * and severe natural disasters, such as floods, heatwaves, wildfires, and storms.</span></div><div><span style="color: green;"> * */</span></div></div><h2>Note:</h2><p>- Unlike ChatGPT, OpenAI API is not free but charges based on the number of input and output characters each of your APIs</p><p>- A programmer can use OpenAI's API to integrate various natural language processing capabilities into their applications, such as text generation, question answering, language translation, sentiment analysis, and more. With the API, You can leverage the power of OpenAI's advanced machine learning models and algorithms without having to build them from scratch. However, note that OpenAI has some usage restrictions for their API, so make sure to review their documentation before integrating it into your project.</p><p><br /></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-16676582890599242942023-02-10T19:36:00.006-08:002023-02-10T19:38:11.432-08:00How to fix error "system is deadlocked on memory" on Vultr VPS<p>The error "<i><span style="color: #cc0000;">system is deadlocked on memory</span></i>" "<i><span style="color: #cc0000;">end Kernel panic - not syncing</span></i>" usually indicates that the system has run out of available memory and processes are unable to allocate additional memory. This error comes from the VPS kernel, so it can be encountered in both Linux or Windows VPS operating systems. To resolve the issue on your Vultr VPS or any other service provider (Linode, Digitalocean ...) you can try the following steps:</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgykH6RVGNGyLTc5a2aJ76MJW0MzjeIILZ5wRYsjnrsFNzjm9WW5dTGPUswBY6f7S7YfGECdB5CLBOkcCX83-fri_lakwL7jHYxh_yi49y2UOaN_3m3QRWENY9BeAGXz9ZsGBl5BwwD9iZ0-x-x2B2xAq3xfTMGG7yBCH24SfaH0qwWof7SQ5quw4wa/s711/end%20Kernel%20panic.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="356" data-original-width="711" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgykH6RVGNGyLTc5a2aJ76MJW0MzjeIILZ5wRYsjnrsFNzjm9WW5dTGPUswBY6f7S7YfGECdB5CLBOkcCX83-fri_lakwL7jHYxh_yi49y2UOaN_3m3QRWENY9BeAGXz9ZsGBl5BwwD9iZ0-x-x2B2xAq3xfTMGG7yBCH24SfaH0qwWof7SQ5quw4wa/w400-h200/end%20Kernel%20panic.jpg" width="400" /></a></div><p><b>Note: </b><i><span style="color: red;">The first thing you need to do when you encounter this error is to quickly backup or snapshot VPS. Because the wrong operations at this time can cause you to lose the data in your VPS. </span></i></p><p><span style="font-size: large;"><b>Case 1 : Error on startup (can not start VPS)</b></span></p><div class="flex flex-grow flex-col gap-3"><div class="min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap"><div class="markdown prose w-full break-words dark:prose-invert light"><ol><li><p>Restart the server: If the issue persists, you can restart your Vultr VPS to clear the memory and resolve the deadlock.</p></li><li><p>Turn off the startup scripts with VPS (in VPS management) that you added (if exist)</p></li><li><p><b><i>Upgrading to a VPS droplet with more memory</i></b>: If other solutions don't work, upgrading to a more configurable VPS plan might solve the situation. Upgrading VPS to a higher droplet at Vultr is easy, safe and does not affect your data or applications.</p></li><li><p>Contact Vultr for support: The last resort or if you are too worried about the current data in the VPS. Experts from Vultr will be able to help you fix the problem</p></li></ol><div><span style="font-size: large;"><b>Case 2: Error during operation (sometimes encounter)</b></span></div><div><span style="font-size: large;"><b><br /></b></span></div><div><ol><li><p>Monitor resource usage: Use the <span style="color: #cc0000;">'</span><code><span style="color: #cc0000;">top'</span>,<span style="color: #cc0000;">'</span></code><span style="color: #cc0000;"> <code>htop'</code></span> command or <span style="color: #cc0000; font-family: courier;">"Task Manager'</span> to monitor the resource usage of your system. Identify the processes that are consuming large amounts of memory and determine if they can be terminated or if their resource usage can be optimized.</p></li><li><p>Kill processes: If a specific process is consuming a large amount of memory and is not responding, terminate it.</p></li><li><p>Increase swap space(Linux): If your system does not have enough physical memory, you can increase the amount of swap space to temporarily address the issue.</p></li><li><p>Optimize application performance: If the issue recurs frequently, you may need to optimize the performance of your applications</p></li></ol></div><p>It's important to monitor your system and its resource usage to identify and resolve issues before they become severe. Consider using a system monitoring tool to automatically alert you to any issues.</p></div></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-52786268988847049432022-11-07T09:08:00.002-08:002022-11-07T09:11:47.825-08:00PHP Convert Hex String to String<h2 style="text-align: left;">How to decode Hex String to String?</h2><p>Ex: Decode Hex String to get JWT token when integrating Apple ID login</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiowhpojhL481EWk96doYD8mYYYtYx4fisvVr3FsWH5YVhzyZXtUhyDVrFgkq3Umtp3FbF8GZ7AIHunSjSykJF9-59ThnGvHxwj_EcraOsvHI0kxeFoYQa9EwCCR7I2kypoCX8QK_jPkTqI6sui93vQSUPRD24BJIl8MLTqcSdCLWHwSnQCfdYYLdgI/s739/php%20hex%20to%20string.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="428" data-original-width="739" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiowhpojhL481EWk96doYD8mYYYtYx4fisvVr3FsWH5YVhzyZXtUhyDVrFgkq3Umtp3FbF8GZ7AIHunSjSykJF9-59ThnGvHxwj_EcraOsvHI0kxeFoYQa9EwCCR7I2kypoCX8QK_jPkTqI6sui93vQSUPRD24BJIl8MLTqcSdCLWHwSnQCfdYYLdgI/s320/php%20hex%20to%20string.jpg" width="320" /></a></div><br /><h3 style="text-align: left;">Solved</h3><p>Hex string is a data type commonly used for transmission over the internet in the lower network layers. In some cases PHP will need to manipulate this data type directly. To handle them, PHP provides us with the "pack" function to manipulate Hex and binary string data.</p><p>Ex:</p>
<pre><code>
$stringHexData = "65794a68624763694f694a49557a49314e694973496e523563434936496b705856434a392e65794a7063334d694f694a6f64485277637a6f764c3246776347786c61575175595842776247557559323974496977695958566b496a6f69593239744c6e4e68625842735a533568634841694c434a6c654841694f6a45324d5459354f4455794e6a4973496d6c68644349364d5459784e4449314d444d334d69776963335669496a6f694d4441774e7a677a4c6d4e6b5a6a45354d47526b4d544130595452685a6a42694e6d49774e6a63784d3249344d4455304d6a52694c6a45304d5451694c434a6a58326868633267694f694a7852557035546d7473556d4a5a626c46565679316b646d6774536b7452496977695a573168615777694f694a7a59573177624756415a3231686157777559323974496977695a57316861577866646d567961575a705a5751694f694a30636e566c4969776959585630614639306157316c496a6f784e6a45324f5441774d7a63794c434a756232356a5a56397a6458427762334a305a5751694f6e527964575573496e4a6c5957786664584e6c636c397a6447463064584d694f6a4a392e764b6c394873303057587463426851796f47326742766a744375394d5878787471674e46777854772d5034";
$stringData = pack("H*",$stringHexData);
echo $stringData;
</code>
</pre>
<p>Result (JWT String): </p>
<pre style="text-align: left;"><code>
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLnNhbXBsZS5hcHAiLCJleHAiOjE2MTY5ODUyNjIsImlhdCI6MTYxNDI1MDM3Miwic3ViIjoiMDAwNzgzLmNkZjE5MGRkMTA0YTRhZjBiNmIwNjcxM2I4MDU0MjRiLjE0MTQiLCJjX2hhc2giOiJxRUp5TmtsUmJZblFVVy1kdmgtSktRIiwiZW1haWwiOiJzYW1wbGVAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiYXV0aF90aW1lIjoxNjE2OTAwMzcyLCJub25jZV9zdXBwb3J0ZWQiOnRydWUsInJlYWxfdXNlcl9zdGF0dXMiOjJ9.vKl9Hs00WXtcBhQyoG2gBvjtCu9MXxxtqgNFwxTw-P4
</code>
</pre>
<h3 style="text-align: left;">More </h3><p>In addition, the "pack" function also supports other data types. </p>
<pre><code>
possible values of format are:
a – string which is NUL-padded
A – string which is SPACE-padded
h – low nibble first Hex string
H – high nibble first Hex string
c – signed character
C – unsigned character
s – signed short (16 bit, machine byte order)
S – unsigned short ( 16 bit, machine byte order)
n – unsigned short ( 16 bit, big endian byte order)
v – unsigned short ( 16 bit, little endian byte order)
i – signed integer (machine dependent byte order and size)
I – unsigned integer (machine dependent byte order and size)
l – signed long ( 32 bit, machine byte order)
L – unsigned long ( 32 bit, machine byte order)
N – unsigned long ( 32 bit, big endian byte order)
V – unsigned long ( 32 bit, little endian byte order)
f – float (machine dependent representation and size)
d – double (machine dependent representation and size)
x – NUL byte
X – Back up one byte
Z – string which is NUL-padded
@ – NUL-fill to absolute position
</code>
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-16762289097608937352022-11-05T02:32:00.003-07:002022-11-05T02:33:15.235-07:00Android - Replace OnLifecycleEvent is deprecated with DefaultLifecycleObserver or LifecycleEventObserver<p>As of androidx.lifecycle version 2.4.0, OnLifecycleEvent is deprecated. Instead Use DefaultLifecycleObserver or LifecycleEventObserver is recommended to use instead. Many android applications that use OnLifecycleEvent such as when integrating Admob Open Ads will receive a warning.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgoDRO8g8aaj2K_9J9rwAR7Irx-n5bbV9Zc3n7PXSl7Xt4D1JLLlFDJRtzCYbGdufX1orB9d1cw-sj1eM7t7gkIBC7H7PvQEmKSRb4UAcajHWqD5prMhdRnLS9z8FQ5KwLN3N22ZLRZ4KaQtyvERZXkCKNuGzMdynMBIuhcNIduKgISo9vkuFWPUM3/s1200/Android%20OnLifecycleEvent%20is%20deprecated.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="675" data-original-width="1200" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgoDRO8g8aaj2K_9J9rwAR7Irx-n5bbV9Zc3n7PXSl7Xt4D1JLLlFDJRtzCYbGdufX1orB9d1cw-sj1eM7t7gkIBC7H7PvQEmKSRb4UAcajHWqD5prMhdRnLS9z8FQ5KwLN3N22ZLRZ4KaQtyvERZXkCKNuGzMdynMBIuhcNIduKgISo9vkuFWPUM3/w400-h225/Android%20OnLifecycleEvent%20is%20deprecated.jpg" width="400" /></a></div><div><br /></div><div>For example, the following code will be warned by Android Studio OnLifecycleEvent is deprecated:</div><div><br /></div>
<pre><code>
/**
* LifecycleObserver methods
*/
@OnLifecycleEvent(ON_START)
public void onStart() {
showAdIfAvailable();
Log.d(LOG_TAG, "onStart");
}
</code></pre><div><b><i><span style="color: red;">
To replace OnLifecycleEvent, follow these instructions:</span></i></b></div>
<h3 style="text-align: left;">First: In build.gradle, please include the LifecycleObserver libraries:</h3>
<pre><code>
apply plugin: 'com.android.application'
dependencies {
...
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.5.1"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0"
}
</code>
</pre>
<p><span style="color: red;"><b><i>Then choose one of the following two alternatives:</i></b></span></p>
<h3 style="text-align: left;">Method 1: replace OnLifecycleEvent with DefaultLifecycleObserver</h3>
<pre style="text-align: left;"><code>
...
import androidx.lifecycle.DefaultLifecycleObserver;
public class MyApplication extends Application implements DefaultLifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
// Register DefaultLifecycleObserver
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
/*Replace for OnLifecycleEvent onStart Event */
@Override
public void onStart(@NonNull LifecycleOwner owner) {
// Perform actions every time the app is reopened
}
}
</code>
</pre>
<h3 style="text-align: left;">Method 2: replace OnLifecycleEvent with LifecycleEventObserver</h3>
<pre style="text-align: left;"><code>
public class MyApplication extends Application implements LifecycleObserver
{
@Override
public void onCreate() {
super.onCreate();
//...
// Register LifecycleObserver
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
//...
}
/*Replace for OnLifecycleEvent onStart Event */
@OnLifecycleEvent(Event.ON_START)
protected void onMoveToForeground() {
// Perform actions every time the app is reopened (moves to foreground)
// appOpenAdManager.showAdIfAvailable(currentActivity);
}
}
</code>
</pre>
<h3 style="text-align: left;">Done !</h3><p>If you have any difficulties, or have any questions, please leave a comment below the article. We will deal with them together. Thanks</p><p><br /></p>Unknownnoreply@blogger.com0Thành phố New York, Tiểu bang New York, Hoa Kỳ40.7127753 -74.005972812.402541463821152 -109.1622228 69.023009136178842 -38.849722799999995tag:blogger.com,1999:blog-7830657759918635336.post-78368208595744334802022-10-21T04:11:00.003-07:002022-10-21T04:11:30.575-07:00Flutter Dart Error: Can't load Kernel binary - Invalid kernel binary<p>During working with Flutter, Error "Can't load Kernel binary" may arise unexpectedly after we make some changes to the application. This error is often confusing for developers because it often does not specify a clear cause. Even if you haven't done anything wrong, mistakes can still appear spontaneously. With luck, cleaning the project can fix this problem, but in many cases it doesn't work.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNPNfv7nxCq1FeKLedAgnsz7l8swqSxlgZJr0flCQkOkCZCFLRxQAIwfPHqVoA9BSwIK8EILGOFGdflC1VH5HFmJqDUn4vBJwaQ_lIa8pkxo12kga1_mlIreZr0sPY_6XqGLkn53BOvRxYcnSSaGtapwG1X7-Q6F34T1VwUeTvAw4edPVUSuh1IlPG/s707/Flutter-Dart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="480" data-original-width="707" height="217" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNPNfv7nxCq1FeKLedAgnsz7l8swqSxlgZJr0flCQkOkCZCFLRxQAIwfPHqVoA9BSwIK8EILGOFGdflC1VH5HFmJqDUn4vBJwaQ_lIa8pkxo12kga1_mlIreZr0sPY_6XqGLkn53BOvRxYcnSSaGtapwG1X7-Q6F34T1VwUeTvAw4edPVUSuh1IlPG/s320/Flutter-Dart.png" width="320" /></a></div><h3 style="text-align: left;">Example of an error message:</h3><pre style="text-align: left;"><code>[ERROR:flutter/shell/common/shell.cc(197)] Dart Error: Can't load Kernel binary: Invalid kernel binary: Indicated size is invalid.
.../runtime/runtime_controller.cc(385)] Could not create root isolate
./android/android_shell_holder.cc(138)] Could not launch engine in configuration.
</code>
</pre><h3 style="text-align: left;">
Reason:</h3><div><div>Android Studio's compiler sometimes doesn't work as expected, so the process of linking libraries to compile Dart source code may encounter errors that distort the project's configuration paths. This results in errors during or after compilation with cryptic errors, even errors reported that seem absurd, unrelated to the problem. (It appears to be similar to the bug in Android Studio's Dart code suggestion function. In many cases Android Studio suggests inserting const or required keywords but inserts these keywords in the wrong place. which it is suggested.).</div><div>Once errors of this type arise, it is often cached in the project, so many times we try to find ways to not handle the error, but sometimes it suddenly disappears and cannot be re-appeared.</div></div><div><br /></div><div><i><span style="color: #cc0000;">"Dart Error: Can't load Kernel binary: Invalid kernel binary: Indicated size is invalid."</span></i></div><h3 style="text-align: left;">Solve it</h3><div>Please try the following steps one by one until the problem is resolved</div><div><br /></div><div><b><i>Step 1: Clear project cache</i></b></div><div><div>Completely close the application in the virtual machine.</div><div>In the main project directory, run the command: "<b><span style="color: #3d85c6;">flutter clean</span></b>"</div><div>Then run "<b><span style="color: #3d85c6;">flutter pub get</span></b>" again to update the libraries. </div><div>Rebuild the project to see if the error has been resolved, You can also try to change a little bit in the application's code to make sure the application has been rebuilt and not loaded from the cache. If the problem is not resolved, try step 2</div></div><div><br /></div><div><b><i>Step 2: Clear Flutter SDK Cache</i></b></div><div><div>Go and delete all files in the cache folder of Flutter SDK.</div><div>"<span style="color: #3d85c6;"><b>...flutter/bin/cache</b></span>"</div><div>As required when installing, the Flutter SDK is usually installed in the C drive. For example: <span style="color: #3d85c6;">C:/flutter</span></div><div>You can also easily find the path to the Flutter SDK in the "<i><span style="color: #cc0000;">.flutter-plugins</span></i>" file inside your project.</div><div>After clearing the cache, run the "<b><span style="color: #3d85c6;">flutter doctor</span></b>" command again to let flutter update the libraries (you can run "<b><span style="color: #3d85c6;">flutter doctor</span></b>" from anywhere).</div><div>Re-open Project to see if the problem has been resolved. If not, continue with step 3.</div></div><div><br /></div><div><b><i>Step 3: Rollback of previous project changes</i></b></div><div><div>Try reversing some of the last changes you made to the project's source code. (Remember to save those changes because you can still use them normally if they're not wrong.) Reversing changes, creating changes in the project's source code can help clear caches and miscompiled configuration fragments.</div><div>If the error is still not resolved when you have reversed the small segments then try some higher level change in the file "<span style="color: #cc0000;"><i>main.dart</i></span>"</div></div><div><br /></div><div><b>Note:</b> In the process of fixing errors, you should not use "<b><span style="color: #cc0000;">hot restart</span></b>"</div><div><br /></div><div>If there is anything more to share in this situation, please leave information in the comment section so we can discuss.</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-37833338138067921762022-08-30T10:59:00.003-07:002022-08-30T10:59:50.353-07:00MariaDB/MySQL - Fix error "Unknown/unsupported storage engine: InnoDB" by using recover mode<p>Although the InnoDB storage engine has mechanisms to preserve data, problems such as power failure or sudden restart can still cause errors in MySQL/MariaDB data. These errors can make the system unable to boot, unable to restore the normal data state. Some of the error messages we often encounter are listed below.</p>
<pre><code>
[Note] InnoDB: Starting shutdown...
[ERROR] InnoDB: Database was not shut down normally!
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[Note] Plugin 'FEEDBACK' is disabled.
[ERROR] InnoDB: Starting crash recovery from checkpoint LSN=1637690
[ERROR] Unknown/unsupported storage engine: InnoDB
[ERROR] Aborting
</code>
</pre>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuh63b_cmuwyCuwhgV2HrSvtAqqs1xH-OdkeJ1cA4O59wrBXkQNge3pc_VTKj_RYI7MeSHs0DDfSRU1RDrMWeIFf8EnzhJqgXWMoePjWFkbT-zfKqZPcKQKUgk4mQVZGOnToBJrLiM3K0HBKCu2lV0kuBZmON8c3WXxju54FYztmg0BnnfYYLM_zY3/s473/mysql-mariadb-innodb.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="187" data-original-width="473" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuh63b_cmuwyCuwhgV2HrSvtAqqs1xH-OdkeJ1cA4O59wrBXkQNge3pc_VTKj_RYI7MeSHs0DDfSRU1RDrMWeIFf8EnzhJqgXWMoePjWFkbT-zfKqZPcKQKUgk4mQVZGOnToBJrLiM3K0HBKCu2lV0kuBZmON8c3WXxju54FYztmg0BnnfYYLM_zY3/s16000/mysql-mariadb-innodb.png" /></a></div><br /><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #121212; font-size: 14.4px;"><br /></span><p></p><p><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #121212; font-size: 14.4px;"><b>Note:</b> Some crashes are caused by other causes but also return the error message "Unknown/unsupported storage engine: InnoDB". In that case, there will be more detailed information in the MySQL log to help you find the cause and handle it. As for the cases where the service can't tell why InnoDB is unsupported or unrecoverable, most of the time, data errors are caused. You can handle that case according to the instructions in this article.</span></p><p><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #121212; font-size: 14.4px;">To use this mode is very simple. You just need to specify the value </span><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #cc0000; font-size: 14.4px;"><i>innodb_force_recovery</i></span><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #121212; font-size: 14.4px;"> in the installation file of MySQL or MariaDB. Then restart the service. MySQL will restart and try to use recovery mode to recover the corrupted data.</span></p>
<pre><code>
[mysqld]
innodb_force_recovery = 1
</code>
</pre>
<p>MySQL has a total of 6 recovery modes in ascending severity. You often choose to get the most suitable mode for your system. Please try again with a higher level if the error is not resolved.
</p>
<pre><code>
1 (SRV_FORCE_IGNORE_CORRUPT)
Lets the server run even if it detects a corrupt page. Tries to make SELECT * FROM tbl_name jump over corrupt index records and pages, which helps in dumping tables.
2 (SRV_FORCE_NO_BACKGROUND)
Prevents the master thread and any purge threads from running. If an unexpected exit would occur during the purge operation, this recovery value prevents it.
3 (SRV_FORCE_NO_TRX_UNDO)
Does not run transaction rollbacks after crash recovery.
4 (SRV_FORCE_NO_IBUF_MERGE)
Prevents insert buffer merge operations. If they would cause a crash, does not do them. Does not calculate table statistics. This value can permanently corrupt data files. After using this value, be prepared to drop and recreate all secondary indexes. Sets InnoDB to read-only.
5 (SRV_FORCE_NO_UNDO_LOG_SCAN)
Does not look at undo logs when starting the database: InnoDB treats even incomplete transactions as committed. This value can permanently corrupt data files. Sets InnoDB to read-only.
6 (SRV_FORCE_NO_LOG_REDO)
Does not do the redo log roll-forward in connection with recovery. This value can permanently corrupt data files. Leaves database pages in an obsolete state, which in turn may introduce more corruption into B-trees and other database structures. Sets InnoDB to read-only.
</code><span style="color: #121212; font-family: Times New Roman;"><span style="font-size: 14.4px; white-space: normal;">
</span></span></pre>
<p><span style="font-size: 14.4px;"><span style="color: #121212;">Finally, after troubleshooting, restore the "</span><span style="color: #cc0000;"><i>innodb_force_recovery</i></span><span style="color: #121212;">" configuration to level 0 and then restart the database service to keep the system working properly.</span></span></p><p><span style="font-size: 14.4px;"><span face="Roboto, Tahoma, Verdana, Arial, Helvetica, sans-serif" style="background-color: #fefefe; color: #121212;"></span></span></p><p><span style="color: #121212;"><span style="font-size: 14.4px;">Also note that this is only a workaround, it may also fail if the data is badly corrupted. Our advice remains that every database system should always have a mechanism to automatically back up data.</span></span></p><p><span style="color: #121212;"><span style="font-size: 14.4px;"><br /></span></span></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-47802186696549429462022-08-21T15:44:00.000-07:002022-08-21T15:44:16.904-07:00[Flutter] Async Function vs Normal Function in Dart, How do they work?<p> "Synchronous", "asynchronous", "await" and "asynchronous functions" are very confusing concepts when used in Flutter. For ease of explanation, in this article we will analyze two types of Flutter functions: Normal Function and Async Function. We will learn how these two types of functions work.</p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2O6Ewv4gyMVoH7eVO-Na5mGmCmP_JUu6K0_amGvriGjGBmiN9Ty7p4-GuqRV2Oc_L0k0BmoXiwXaVB7wL3sXZHNv0HylwZs5leCQVZ9URF5NBNodXCzr-9YYUvTIKE5hlR_GwvVqAy0TWX2Gg6IoXviBqyP1r-WMN8HILm9_1NO6xi8aliuOyJxcu/s442/dart-async-function.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="442" data-original-width="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2O6Ewv4gyMVoH7eVO-Na5mGmCmP_JUu6K0_amGvriGjGBmiN9Ty7p4-GuqRV2Oc_L0k0BmoXiwXaVB7wL3sXZHNv0HylwZs5leCQVZ9URF5NBNodXCzr-9YYUvTIKE5hlR_GwvVqAy0TWX2Gg6IoXviBqyP1r-WMN8HILm9_1NO6xi8aliuOyJxcu/s16000/dart-async-function.png" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Ficture 1: Flutter/Dart async work flow</td></tr></tbody></table>
<h3 style="text-align: left;">Normal Function and Async Function</h3>
<div>The Dart language that Flutter uses inherits many features from Javascript. Dart's handling of synchronous and asynchronous jobs is also very similar to Javascript. There are two types of functions in the Dart language:</div>
<h4 style="text-align: left;">* Normal Function:</h4>
<p>A function in Dart is declared as follows:</p>
<pre><code>
String doSomthing(){
print('This is a normal Function');
// do somthing
return 'Done!';
}</code></pre>
<h4 style="text-align: left;">* Async Function:</h4>
<p>Async functions are functions declared with the async keyword at the end. </p>
<pre><code>
String getApiContent() async{
print('Async function loading api from Server');
final response = await http.get(Uri.parse('https://codesiri.com/search/?q=api'));
return response.body;
}
</code></pre>
<div>The difference of Async functions from normal functions is that in an Async function you will be able to use the await keyword to wait for another function to finish executing. Also, an Async doesn't return an immediate result, it just returns a Future.</div>
<h3 style="text-align: left;">Dart is an asynchronous language</h3>
<div>By default, Dart always tries to process everything asynchronously whether it is an async function or a normal function. Let's analyze the example depicted in Figure 1.</div>
<div style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2O6Ewv4gyMVoH7eVO-Na5mGmCmP_JUu6K0_amGvriGjGBmiN9Ty7p4-GuqRV2Oc_L0k0BmoXiwXaVB7wL3sXZHNv0HylwZs5leCQVZ9URF5NBNodXCzr-9YYUvTIKE5hlR_GwvVqAy0TWX2Gg6IoXviBqyP1r-WMN8HILm9_1NO6xi8aliuOyJxcu/s442/dart-async-function.png"><img border="0" data-original-height="442" data-original-width="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2O6Ewv4gyMVoH7eVO-Na5mGmCmP_JUu6K0_amGvriGjGBmiN9Ty7p4-GuqRV2Oc_L0k0BmoXiwXaVB7wL3sXZHNv0HylwZs5leCQVZ9URF5NBNodXCzr-9YYUvTIKE5hlR_GwvVqAy0TWX2Gg6IoXviBqyP1r-WMN8HILm9_1NO6xi8aliuOyJxcu/s16000/dart-async-function.png" /></a></div>
<p>
Like other programming languages, DArt will also process the source code line by line from top to bottom. Even when encountering an Async function it continues to deal with the same logic. That's why in the example we see line 1 and line 2 in asyn function callApi still being executed. "Async" appears only when an actual async event occurs (http.get).
</p><p>Inside an Async function when encountering a "real" async event (a predefined async event by Dart, not a self-created async function) there are 2 options.</p><p><i>Case 1:</i> you can use the await keyword to wait until the async event completes and return the result. The current async function will break at that location. You did not read it wrong, Exactly, the async function will be interrupted at that point, not a new thread or process is initialized to run in the background at this time to continue the async task we are expecting. (You can use additional tools like Charles to verify this.) Of course Dart has kept in mind that there is an Async event still pending. Next Dart will return to the main program to continue to execute the next lines of code. That's why we see the sleep function being executed immediately followed by lines 3, 4, 5 being executed. After the main program has completed, Dart returns to continue processing asynchronous events and the next line of code in the async function.</p><p><i>Case 2:</i> if you don't use await keyword before async event. Just like the case of 1 Dart will remember the pending asynchronous event, but next Dart will continue to execute the next code in the current async function as if nothing happened.</p>
<pre><code>
import 'package:http/http.dart' as http;
import 'dart:io';
void main(){
print('Line 1 - done !');
var apiResult = callApi();
sleep(Duration(seconds: 15)); // Test to monitor async task
print('Line 3 - done !');
normalFunction();
print('Line 5 - done !');
}
Future<string> callApi() async{
print('Line 2 - Async Function callApi called');
normalFunction2();
print('==>A: Line 2 - Line 2 function callApi');
final response = await http.get(Uri.parse('https://codesiri.com/search/?q=api'));
print('==>A: Line 3 - call api done !');
print('==>A: Line 4 - done !');
return 'Done !';
}
void normalFunction1(){
print('Line 4 - This is normalFunction1');
}
void normalFunction2(){
print('==>A: Line 1 - This is normalFunction2');
}
</string></code></pre>
<p>
Result:
</p>
<pre><code>
I/flutter (21546): Line 1 - done !
I/flutter (21546): Line 2 - Async Function callApi called
I/flutter (21546): ==>A: Line 1 - This is normalFunction2
I/flutter (21546): ==>A: Line 2 - Line 2 function callApi
I/flutter (21546): Line 3 - done !
I/flutter (21546): Line 4 - This is normalFunction1
I/flutter (21546): Line 5 - done !
I/flutter (21546): ==>A: Line 3 - call api done !
I/flutter (21546): ==>A: Line 4 - done !<span style="font-family: Times New Roman;"><span style="white-space: normal;">
</span></span></code></pre>
<h3 style="text-align: left;">Dart is a single-threaded programming language</h3><p>The reason Dart has to handle such complex asynchronous events is because it was designed to be a single-threaded language. By default our entire Flutter application runs on a single Dart thread. You will also still be able to create additional threads to actively handle parallel or background tasks, but then you will have to deal with the problems that arise.</p><h3 style="text-align: left;">Summary</h3><p>When it encounters an async event, Dart will always push it to the bottom to wait for it to be processed. If there is await before the async call, all code that follows will also be pushed to the bottom to wait with it. Otherwise the next lines of code will still be treated as if nothing happened. Understanding how Dart's async works will help you optimize your application.</p>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-62363032811584939422022-07-29T07:13:00.001-07:002022-07-29T07:13:26.160-07:00[Flutter] Fix install error "cmdline-tools component is missing" and "Android license status"<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim2L_yd23yjrXmjsMADS2nqECLgo1I_BiBZHR6qiI9OmYYUsw04hFmEMFNtyB4lR5t3XXfkAQooEjwt-WhlDMScAYTrGn9EEaphPKtQllVTQuD8qO4SZkRIE-Dgs9RSHi4nVUTlnlELFqGW79g5Ci9nDIpvePgkHD8xwg5WModrmlysQKgnGb0pOde/s1037/Flutter%20Windows.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Flutter on windows" border="0" data-original-height="584" data-original-width="1037" height="143" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim2L_yd23yjrXmjsMADS2nqECLgo1I_BiBZHR6qiI9OmYYUsw04hFmEMFNtyB4lR5t3XXfkAQooEjwt-WhlDMScAYTrGn9EEaphPKtQllVTQuD8qO4SZkRIE-Dgs9RSHi4nVUTlnlELFqGW79g5Ci9nDIpvePgkHD8xwg5WModrmlysQKgnGb0pOde/w254-h143/Flutter%20Windows.jpg" width="254" /></a></div><p>Command line tools are very useful during android app development with Flutter. In some cases you may have difficulty installing it due to version incompatibility or conflicts with other software. After installing cmdline-tools you can continue to confirm the Android license.</p><h4 style="text-align: left;"><i>Sample Flutter doctor analytic info:</i></h4><p></p><blockquote><div style="text-align: left;">C:\Users>flutter doctor<br />Doctor summary (to see all details, run flutter doctor -v):<br />[√] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.19043.1826], locale en-US)<br />[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)<br /> X cmdline-tools component is missing<br /> Run `path/to/sdkmanager --install "cmdline-tools;latest"`<br /> See https://developer.android.com/studio/command-line for more details.<br /> X Android license status unknown.<br /> Run `flutter doctor --android-licenses` to accept the SDK licenses.<br /> See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.<br />[√] Chrome - develop for the web<br />[X] Visual Studio - develop for Windows<br /> X Visual Studio not installed; this is necessary for Windows development.<br /> Download at https://visualstudio.microsoft.com/downloads/.<br /> Please install the "Desktop development with C++" workload, including all of its default components<br />[√] Android Studio (version 2021.2)<br />[√] VS Code (version 1.69.2)<br />[√] Connected device (3 available)<br />[√] HTTP Host Availability<br />! Doctor found issues in 2 categories.</div></blockquote><p></p><p>As suggested, you just need to run the command: <span style="color: #cc0000;"><b>Run `path/to/sdkmanager --install "cmdline-tools;latest"`</b></span>. But there might be an error: <b style="color: #cc0000;">Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema. </b>In this case we can fix it by installing cmdline-tools through Android Studio. This method is always applicable and very easy to do.</p><p><b><span style="font-size: medium;">Step 1: </span></b>In the Android Studio software, you click on the menu <span style="color: #cc0000;"><b>Tools</b></span> >> <b><span style="color: #cc0000;">SDK Manager</span></b></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-3dkiJBZb5pYULfFz6EmupIs-Yrci1fgJ1yMYvbzONEOCVzwkLmLXtCAVsYlJ51TVDlKwFywIoIH7EvvPmCFMRzY2N7Jx16TcVq9deIceN44seDSWlb0TVzDPVpdAeqyHB3wpUaeMVp7U8ceJBcS8uwfH_58sr48E9psxUuSuhlHpLQs0fALLAyIw/s527/Android%20Studio%20SDK%20Manager.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="454" data-original-width="527" height="276" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-3dkiJBZb5pYULfFz6EmupIs-Yrci1fgJ1yMYvbzONEOCVzwkLmLXtCAVsYlJ51TVDlKwFywIoIH7EvvPmCFMRzY2N7Jx16TcVq9deIceN44seDSWlb0TVzDPVpdAeqyHB3wpUaeMVp7U8ceJBcS8uwfH_58sr48E9psxUuSuhlHpLQs0fALLAyIw/s320/Android%20Studio%20SDK%20Manager.jpg" width="320" /></a></div><b><span style="font-size: medium;">Step 2: </span></b>In the SDK Manager setting screen, select the "SDK Tools" tab. Then tick "Android SDK Command-line Tools (lastest)". Finally click "Apply" to start the installation.<div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihil5EHOdpW-aXaHvWgX3xUO1FZfv1s_383DKamuJ_nQ8nBqo3M5h-HBtyRdfKrmgXeY1aukaLFYNxsbggSzIvTGMkBxbX9V9mzLNLuCgrL8Me0XGTBw0htxj5hCGt9HBZSOk17x73zCpLJFOjTk0Qfh3QjZSGrNQ9wr0ATdiu3aotzXxbUNT_sqAw/s1207/Android%20Studio%20SDK%20Manager%20install%20cmd.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="893" data-original-width="1207" height="366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihil5EHOdpW-aXaHvWgX3xUO1FZfv1s_383DKamuJ_nQ8nBqo3M5h-HBtyRdfKrmgXeY1aukaLFYNxsbggSzIvTGMkBxbX9V9mzLNLuCgrL8Me0XGTBw0htxj5hCGt9HBZSOk17x73zCpLJFOjTk0Qfh3QjZSGrNQ9wr0ATdiu3aotzXxbUNT_sqAw/w494-h366/Android%20Studio%20SDK%20Manager%20install%20cmd.jpg" width="494" /></a></div><br /><p><b><span style="font-size: medium;">Step 3:</span></b> After installing "Command-line Tools" you can open a command line window to continue to confirm "Android licenses"</p><p></p><blockquote>flutter doctor --android-licenses</blockquote><p></p><p><b><span style="font-size: medium;">Done !</span></b> Check Flutter doctor to make sure everything is fully installed. Good luck.</p><p><br /></p></div>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-7830657759918635336.post-14932391739606642692022-07-26T03:37:00.006-07:002022-07-26T07:30:07.619-07:00[Laravel] How to find out which Class and File are behind a Facade<p>Facades in Laravel allow us to call libraries very conveniently. However, developers who are new to a project will find it difficult to investigate errors because Laravel Error Handling does not specify which class caused the error. In particular, the problem is very common with Facades that have many services available. Ex: DB, Image, Auth, Cache, Mail...</p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwKOfgUQRrc6TtHDGTGy5vknFydn_BkUsMFWb3ft8Ilqg0QkAkqc1-LO0xivGQnf5HSguvNlyvJxOYnsZdkb-kcnlDBsqPSbM6vx-ykp8Pf9U3Nre3LQl92kdB87lwlRIPIcnJzpPdvIoaosw0EiSQ3MUE6hcv749WlKe-dj7nobV4Q_X80KkDokU9/s631/Laravel%20Facade%20Flow.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="411" data-original-width="631" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwKOfgUQRrc6TtHDGTGy5vknFydn_BkUsMFWb3ft8Ilqg0QkAkqc1-LO0xivGQnf5HSguvNlyvJxOYnsZdkb-kcnlDBsqPSbM6vx-ykp8Pf9U3Nre3LQl92kdB87lwlRIPIcnJzpPdvIoaosw0EiSQ3MUE6hcv749WlKe-dj7nobV4Q_X80KkDokU9/w400-h260/Laravel%20Facade%20Flow.png" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Laravel Facade Flow</td></tr></tbody></table><h2 style="clear: both; text-align: left;">* Quick debug:</h2><div class="separator" style="clear: both; text-align: left;">To find out the PHP class behind a Facade that you don't know where it is, you can debug it with the following code:</div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both; font-style: italic;"></div><blockquote><div class="separator" style="clear: both;"><span style="color: #cc0000;">$behindClass = get_class(\<<Facade>>::getFacadeRoot());</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">$classReflector = new \ReflectionClass($behindClass);</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">dd($behindClass, $classReflector->getFileName(), $classReflector);</span></div></blockquote><div class="separator" style="clear: both; font-style: italic;"></div></div><div class="separator" style="clear: both; text-align: left;"><i>ex:</i> </div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"></div><blockquote><div class="separator" style="clear: both;"><span style="color: #cc0000;">$behindClass = get_class(\Mail::getFacadeRoot());</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">$classReflector = new \ReflectionClass($behindClass);</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">dd($behindClass, $classReflector->getFileName(), $classReflector);</span></div></blockquote><div class="separator" style="clear: both;"></div></div><div class="separator" style="clear: both; text-align: left;"><div style="font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #cc0000;"><b></b></span></div></div></div><div class="separator" style="clear: both; text-align: left;"><i>result:</i></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQEPKnBLCWscbg7p0HKZXyhHTHh5XTFXXgdEMISkcdgbAGSIdUKSCxFsMx0rizD4JU0GIdy2cc_alcE6SNMHKwp9_dvSSUJQRwz-_yqv7YZJaeF2889HHt1vlajgz70rPAI13xIW0dYGCXg50-IpejhDKpclLlwdG3HZVP2wSYlGa-jCMBex44VLhu/s665/Facede%20file.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="268" data-original-width="665" height="161" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQEPKnBLCWscbg7p0HKZXyhHTHh5XTFXXgdEMISkcdgbAGSIdUKSCxFsMx0rizD4JU0GIdy2cc_alcE6SNMHKwp9_dvSSUJQRwz-_yqv7YZJaeF2889HHt1vlajgz70rPAI13xIW0dYGCXg50-IpejhDKpclLlwdG3HZVP2wSYlGa-jCMBex44VLhu/w400-h161/Facede%20file.jpg" width="400" /></a></div><h2 style="text-align: left;">* Understand how the Facade is loaded by Laravel:</h2><div><div class="separator" style="clear: both; text-align: left;">To find the underlying class behind a Laravel Facade without debugging you need to understand how a Facade is installed and loaded into Laravel.</div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;">Facade looks magical, but it's actually based on PHP's built-in class_alias feature. You can see in the project's config file (/config/app.php) there is a block to declare the Facade class alias.</div><div class="separator" style="clear: both;">Ex:</div><div class="separator" style="clear: both;"><div class="separator" style="clear: both;"></div><blockquote><div class="separator" style="clear: both;"><span style="color: #cc0000;">'</span><b><span style="color: #2b00fe;">aliases</span></b><span style="color: #cc0000;">' => [</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> ....</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> '</span><b><span style="color: #2b00fe;">Lang</span></b><span style="color: #cc0000;">' => Illuminate\Support\Facades\Lang::class,</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> '</span><b><span style="color: #2b00fe;">Log</span></b><span style="color: #cc0000;">' => Illuminate\Support\Facades\Log::class,</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> '</span><b><span style="color: #2b00fe;">Mail</span></b><span style="color: #cc0000;">' => Illuminate\Support\Facades\Mail::class,</span></div></blockquote><div class="separator" style="clear: both;"></div></div></div><div class="separator" style="clear: both; text-align: left;">If you open the class Illuminate\Support\Facades\Mail you will see that the class is almost completely empty. It has very little source code and doesn't really handle any features. In this file, the most special thing we see is the getFacadeAccessor() function and this function does not process anything but just returns a simple string like "<b><i><span style="color: #cc0000;">mailer</span></i></b>". This function is present in all Facades.</div><div class="separator" style="clear: both; text-align: left;"></div><blockquote><div class="separator" style="clear: both; text-align: left;"><span><span style="color: #cc0000;">protected static function </span><b><span style="color: #2b00fe;">getFacadeAccessor</span></b><span style="color: #cc0000;">()</span></span></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"><span style="color: #cc0000;">{</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> <span> </span> return '</span><span style="color: #2b00fe;">mailer</span><span style="color: #cc0000;">';</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">}</span></div></div></blockquote><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"><b><span style="color: #cc0000;"></span></b></div></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;">It looks magical, but it's not really here. <b><i><span style="color: #cc0000;">Illuminate\Support\Facades\Mail</span></i></b> inherits another class, <b><i><span style="color: #cc0000;">Illuminate\Support\Facades\Facade::class</span></i></b> . In this superclass Laravel will call <b><i><span style="color: #cc0000;">getFacadeAccessor()</span></i></b> to get the name of the actual service that has been configured by laravel for this facade. The example here is 'mailer'. Laravel will then call the 'mailer' service to return the Facade call - <b><i><span style="color: #cc0000;">app('mailer')</span></i></b>.</div><div class="separator" style="clear: both;">You may be wondering that until now we still do not know where the <b><span style="color: #cc0000;"><i>'mailer' </i></span></b>service is located and how it is declared in Laravel.</div><div class="separator" style="clear: both;">Normally, every Laravel service needs to be declared through a Provider and then be available anywhere. Facades are treated the same way. Usually when you install a service pack from outside you will see in the source code of that service pack in addition to the Class Facade there will be a Class Provider to declare the service with Laravel. This means that once we have found information about Facade's extension in <b><i><span style="color: #cc0000;">config/main.php</span></i></b>, we can next look for Class Provider in that extension's source code to see the exact services that have been provided. How is it declared and what files it leads to.</div></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"><i>Ex:</i></div><div class="separator" style="clear: both;"></div><blockquote><div class="separator" style="clear: both;"><span style="color: #cc0000;">class <b>MailServiceProvider</b> extends <b>ServiceProvider</b> implements <b>DeferrableProvider</b></span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;">{</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> public function register()</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> {</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> $this->registerSwiftMailer();</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> $this->registerIlluminateMailer();</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> $this->registerMarkdownRenderer();</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> }</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> protected function registerIlluminateMailer()</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> {</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> $this->app->singleton('</span><span style="color: #2b00fe;"><b>mailer</b></span><span style="color: #cc0000;">', function ($app) {</span></div><div class="separator" style="clear: both;"><span style="color: #cc0000;"> $config = $app->make('config')->get('mail');</span></div></blockquote><div class="separator" style="clear: both;"></div></div><div class="separator" style="clear: both; text-align: left;">Usually the Providers will be installed automatically when we add the extension package to the project. In addition, Providers can also be manually declared in "<b><span style="color: #cc0000;"><i>config/app.php</i></span></b>". There are some services that are core of Laravel, so by default they are declared when laravel starts. For example, with the 'mailer' service we just learned about. For core services, the Facade location and the service source code may be different. As with most package that you install from a third party, the Facade source code, Facade Provider, and Facade-related files are all in the same place in Vendors.</div><h3 style="clear: both; text-align: left;"><b>* Note:</b></h3><div class="separator" style="clear: both; text-align: left;">we can read the source code in the "vendor" to understand and find a way to fix the error faster, but it is absolutely not recommended to edit the source code directly in the "vendor".</div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-84690617000365056412022-05-28T00:01:00.002-07:002022-05-28T00:01:47.678-07:00[Windows 7] Your connection is not private NET::ERR_CERT_AUTHORITY_INVALID for all HTTPS sites<div>Recently, Windows 7 users reported an error that they could not access the websites "Your connection is not private", "NET::ERR_CERT_AUTHORITY_INVALID". This error is encountered in all browsers on the machine. And it is encountered only when accessing websites with https. In other cases, the website displays a blank page because the static content files cannot be loaded. This error is encountered because the customer's Windows 7 operating system is not fully updated with security patches.</div><div><br /></div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib98UzPwLkK_K52Ng5U0eh_SpfKAVozG75daGEz2Q-xBQn9uHJk55Jt4WNmVUivlLmasON_MkX47ybxeYK5-VjjYsCY3G7bkWZoEjEiRZJMp3XI7FsOe07k1q6U0bMdG-SVTMkRa81OS5QLZG7hWX8B_Oa1MjlXX1_mvUsbfZy5b_fB2Z2QtQDiDup/s898/Your%20connection%20is%20not%20private.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="592" data-original-width="898" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib98UzPwLkK_K52Ng5U0eh_SpfKAVozG75daGEz2Q-xBQn9uHJk55Jt4WNmVUivlLmasON_MkX47ybxeYK5-VjjYsCY3G7bkWZoEjEiRZJMp3XI7FsOe07k1q6U0bMdG-SVTMkRa81OS5QLZG7hWX8B_Oa1MjlXX1_mvUsbfZy5b_fB2Z2QtQDiDup/s320/Your%20connection%20is%20not%20private.png" width="320" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><span style="text-align: left;"><i>SSL certificate error on all browsers, for all HTTPS sites</i></span></td></tr></tbody></table><h1 style="text-align: left;">Solved</h1><div>To fix this, If you are using windows 7, please install security update KB3004394 from Microsoft.</div><div>You can download the KB3004394 package from the official Microsoft website using the following link:</div><div><br /></div><div><div><a href="https://www.microsoft.com/en-sg/download/details.aspx?id=45588" target="_blank">KB3004394 Update for Windows 7 X86 (32 Bit)</a></div><div><a href="https://www.microsoft.com/en-us/download/details.aspx?id=45633" target="_blank">KB3004394 Update for Windows 7 X64 (64 Bit)</a></div></div><div><br /></div><div>To install the update successfully, Windows must have the Windows Update feature enabled.</div><div><br /></div><div><div>After installing the update above, please clear your browser's browsing history data and restart your computer. HTTPS sites can now be accessed normally.</div><div>In addition, you should also install all the latest updates for your windows 7 operating system through the Windows Update feature. This will help keep your computer safe and less prone to problems.</div></div><div><br /></div><div><b>Done !</b></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-69058868223661893442022-05-12T04:36:00.007-07:002022-05-14T09:17:14.489-07:00[Solved] SSH and Gitlab negotiate error "no matching host key type found"<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDJ7r7M2rzth1doxypuFyD7b6jbLJYRv4V8ufRJjBBGhMv-fd_bIC5cmU3HIERupnMD42S77uwHVw1uWEoC-nJif9RwVBgnsy_aXOu2TdeVM58VBVzebTFuOymbDGyn0tB7Zoe-h8w9iRL1fazNUHfuz17bTgtEB6TK1QIQUo-qkUAcWcWuf6RMQjT/s546/Ubuntu%20Ssh%20and%20Git.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="300" data-original-width="546" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDJ7r7M2rzth1doxypuFyD7b6jbLJYRv4V8ufRJjBBGhMv-fd_bIC5cmU3HIERupnMD42S77uwHVw1uWEoC-nJif9RwVBgnsy_aXOu2TdeVM58VBVzebTFuOymbDGyn0tB7Zoe-h8w9iRL1fazNUHfuz17bTgtEB6TK1QIQUo-qkUAcWcWuf6RMQjT/s320/Ubuntu%20Ssh%20and%20Git.jpg" width="320" /></a></div><p>When using new Linux operating systems like Ubuntu 22.04 you may have trouble with SSH when you want to connect to old Linux servers.</p><p></p><blockquote><span style="color: red;"><i><b>Unable to negotiate with ***.***.***.*** port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss</b></i></span></blockquote><p></p><p>This error can be encountered when you directly access an old server via SSH. Or when you use Git, SVN, or any other software that uses the SSH protocol.</p><h2 style="text-align: left;">Reason</h2><p>To be able to make an ssh connection, the Server and the client need to negotiate a secure connection encryption method. That is to find an encryption method that both the server and the client support. OpenSSH in older OS versions like Centos 6 only supports the old encryption standards ssh-rsa and ssh-dss. These 2 encryption standards are outdated and potentially dangerous. Therefore, the new version of OpenSSH disables these encryptions by default. Newer encryption commonly used is ssh-ed25519, ecdsa-sha2 ...</p><h2 style="text-align: left;">Solved</h2><p>To solve this error, you need to configure ssh on the new server to accept the old encryption standards as ssh-rsa or ssh-dss (just 1 is enough). We suggest 2 solutions to do just that.</p><h3 style="text-align: left;"><u>Solution 1:</u></h3><p>Enable dss or rsa encryption for ssh on your new server.</p><p>To do so open the file "<i>~/.ssh/config</i>"</p><p></p><blockquote><b><span style="color: #20124d;"><i>vi ~/.ssh/config</i></span></b></blockquote><p></p><p>Then add the following content to the file (change <i style="color: #20124d;">ssh-rsa </i><span style="color: #20124d;">to</span><i style="color: #20124d;"> </i><span style="color: #20124d;"><i>ssh-dss</i></span> if your old server only support it)</p><div></div><blockquote><div><span style="color: #20124d;"><i>Host *</i></span></div><div><span style="color: #20124d;"><i>HostkeyAlgorithms +ssh-rsa</i></span></div><div><span style="color: #20124d;"><i>PubkeyAcceptedKeyTypes +ssh-rsa</i></span></div></blockquote><div></div><p><b>Done !</b> Now you can connect ssh to old servers via terminal normally. However, if you are using Git over ssh with a privateKey file, this will not work (to solve see solution below).</p><p><b>Tip:</b> <i>you can also restrict opening this encryption method only to a certain ip by substituting that ip in the "Host: oldserverIP" section. This will make your server more secure.</i></p><h3 style="text-align: left;"><u>Solution 2:</u></h3><p>Enable dss or rsa encryption only when a connection is needed by adding a parameter to the ssh connect statement.</p><p><i>Ex: </i></p><p></p><blockquote><i><span style="color: #20124d;">ssh 123.123.123.123</span></i></blockquote><p></p><p>become</p><p></p><blockquote><i><span style="color: #20124d;">ssh -oHostKeyAlgorithms=+ssh-rsa -oPubkeyAcceptedAlgorithms=+ssh-rsa </span></i><i><span style="color: #20124d;">123.123.123.123</span></i></blockquote><p></p><p><b>Done !</b></p><h2 style="text-align: left;">Fix negotiate error for Git/Gitlab via ssh privateKey file</h2><p>With git or edit the config file as follows</p><p><i><span style="color: #20124d;"></span></i></p><blockquote><i><span style="color: #20124d;">vi yourProjectPath/.git/config</span></i></blockquote><p></p><p></p><blockquote style="text-align: left;"><div style="text-align: left;"><span style="color: #20124d;"><i>[core]<br /> repositoryformatversion = 0<br /> filemode = true<br /> bare = false<br /> logallrefupdates = true<br /><span style="white-space: pre;"> </span>sshCommand = ssh -oHostKeyAlgorithms=+ssh-rsa -oPubkeyAcceptedAlgorithms=+ssh-rsa -oIdentitiesOnly=yes -i /yourPath/privateKeyFile.ppk -F /dev/null<br />...</i></span></div></blockquote><div></div><div><b>Done !</b> Your Git can now connect to the server normally and it automatically uses the privateKey file to log in instead of having to enter a password.</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-11049222341096847372022-04-15T04:41:00.002-07:002022-04-15T06:29:36.939-07:00Laravel 9 Error: Undefined constant Illuminate\Http\Request::HEADER_X_FORWARDED_ALL<p>Solve the error when upgrading the system to laravel 9 or 10. "Undefined constant Illuminate\Http\Request::HEADER_X_FORWARDED_ALL"</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYx0qGJL8BLm6qMpszaCrLeLzOpKoP7yCyYSBDbdPgeKtEenO0qkkDCTAiTJH-lcD7Eo7yWF-LPavGtKt-Rq3TZ7Vhh6_OpOU-YD-R69UnOPR10iI6PfT2L3gq7XcEJTaBnyWM_R1QOtlOyWiCo9167aHLebMH7zbF_FRpxPIvZz_kNkQ1jJqDuOt7/s788/Laravel%209.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="443" data-original-width="788" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYx0qGJL8BLm6qMpszaCrLeLzOpKoP7yCyYSBDbdPgeKtEenO0qkkDCTAiTJH-lcD7Eo7yWF-LPavGtKt-Rq3TZ7Vhh6_OpOU-YD-R69UnOPR10iI6PfT2L3gq7XcEJTaBnyWM_R1QOtlOyWiCo9167aHLebMH7zbF_FRpxPIvZz_kNkQ1jJqDuOt7/s320/Laravel%209.jpg" width="320" /></a></div><p><b><u>Reason: </u></b></p><p>As of Laravel 9, the framework switched to using a built-in middleware to handle proxy queries instead of Fideloper\Proxy\TrustProxies. So when upgrading from lower versions like laravel 5.8, laravel 8 to laravel 9 we also need to edit to replace this middleware. If you don't make changes you will get an error when you run "<b>composer update</b>" and you won't be able to access the website</p><p><b><u>Solve the problem:</u></b></p><p><b>Step 1:</b> Edit your current TrustProxies Middleware (app/Http/Middleware/TrustProxies.php)</p><p><b>Step 2: </b>Update middleware according to the following example</p><div style="text-align: left;"><blockquote><?php<br />namespace App\Http\Middleware;<br />use Illuminate\Http\Middleware\TrustProxies as Middleware;<br />use Illuminate\Http\Request;<br />class TrustProxies extends Middleware<br />{<br /> /**<br /> * The trusted proxies for this application.<br /> *<br /> * @var array<int, string>|string|null<br /> */<br /> protected $proxies;<br /> /**<br /> * The headers that should be used to detect proxies.<br /> *<br /> * @var int<br /> */<br /> // Before...<br /> // <strike>protected $headers = Request::HEADER_X_FORWARDED_ALL;<br /></strike> // After...<br /> protected $headers =<br /> Request::HEADER_X_FORWARDED_FOR |<br /> Request::HEADER_X_FORWARDED_HOST |<br /> Request::HEADER_X_FORWARDED_PORT |<br /> Request::HEADER_X_FORWARDED_PROTO |<br /> Request::HEADER_X_FORWARDED_AWS_ELB;<br />}</blockquote></div><p><b>Step 3: </b>Remove Fideloper TrustProxies from composer file</p><p></p><blockquote>composer remove fideloper/proxy</blockquote><p></p><p><b>Step 4:</b> Done ! run "<b>composer update</b>" to complete update</p><p><br /></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-35117575331406251792021-08-20T04:08:00.000-07:002021-09-12T02:31:55.729-07:00[Tips] How to use Cloudflare Free SSL for Socket.io Server<p>Free SSL is a very interesting feature of Cloudflare. Cloudflare SSL has full support for WebSocket protocol. However, if you are using the web in conjunction with a socket.io server on the same server, you may encounter problems with the ssl port. Because the default port for ssl is always 443 but it is already used by the web server.</p><p>There are many ways to handle this problem, here I will guide you in a very simple way. That's how to configure Socket.io SSL through a proxy using Apache or Nginx.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6bj0vybqiHfEGeMZilBZeOd2ZjShyIPQbneCDUKE9DUEzrSWMhvCp8qOpHIDycaz-zlNK3GAZr-L5UZu2HZC5bS31Zb-ttoL8GznXHi1zaqltwIp2eOXDroy3ki3bluB_0GbZqUyywbs/s800/cloudflare-socketio.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="368" data-original-width="800" height="147" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6bj0vybqiHfEGeMZilBZeOd2ZjShyIPQbneCDUKE9DUEzrSWMhvCp8qOpHIDycaz-zlNK3GAZr-L5UZu2HZC5bS31Zb-ttoL8GznXHi1zaqltwIp2eOXDroy3ki3bluB_0GbZqUyywbs/s320/cloudflare-socketio.jpg" width="320" /></a></div><br /><h2 style="text-align: left;">Prepare:</h2><p>NodeJs SocketIO server is listen on port 8088</p><p>Webserver (Apache or Nginx) is listen on port 80 and 443</p><h2 style="text-align: left;">Step 1: SocketServer config</h2><p><b>Configure NodeJs SocketIO server to run in long polling mode without ssl on a certain port, eg port 8088.</b></p><p><i>Eg:</i></p><div style="text-align: left;"></div><blockquote><div style="text-align: left;">var app = require('express')(); //npm install express<br />var http = require('http').createServer(app);<br />var socketServer = require('socket.io')(http, { //npm install socketio<br /><span style="white-space: pre;"> </span>cors: {<br /><span style="white-space: pre;"> </span>origin: "*",<br /><span style="white-space: pre;"> </span>methods: ["GET", "POST"]<br /><span style="white-space: pre;"> </span>},<br /> transports: ['polling']<br />});<br />http.listen(8088, () => {<br /><span style="white-space: pre;"> </span>console.log('listening on port 8088');<br />});</div><p style="text-align: left;"></p></blockquote><p style="text-align: left;"><b>Configure socket.io client to use 'polling' mode</b></p><p style="text-align: left;"><i>Eg:</i></p><div style="text-align: left;"><blockquote>var socket = io('https://subdomain.yourdomain.com', { });<br /><span style="white-space: pre;"> </span>socket.on('connect', function () {<br /><span style="white-space: pre;"> </span>console.log('connected');<br />});</blockquote></div><h2 style="text-align: left;">Step 2: Configure virtualhost proxy for Socket Server</h2><p>Configure virtual host proxy to forward port 80 from cloudflare to the actual port of our Socket Server <i>(I listen on port 80 because I am using Cloudflare flexible ssl, if you use Cloudflare full ssl or full strict ssl then listen on port 443 like your other virtualhost)</i></p><p><b>Apache:</b></p><div style="text-align: left;"><blockquote><VirtualHost *:80><br /> ServerAdmin admin@yourdomain.com<br /> ProxyPreserveHost On<br /> ServerName subdomain.yourdomain.com<br /> ProxyPass / http://127.0.0.1:8088/<br /> ProxyPassReverse / http://127.0.0.1:8088/<br /></VirtualHost></blockquote></div><p><b>Nginx:</b></p><div style="text-align: left;"><blockquote>server {<br /> listen 80;<br /> server_name subdomain.yourdomain.com;<br /> location / {<br /> proxy_set_header X-Forwarded-For $remote_addr;<br /> proxy_set_header Host $http_host;<br /> proxy_pass http://127.0.0.1:8088;<br /> }<br />}</blockquote><h2 style="text-align: left;">Done !</h2><p><i>Remember to reload your web server </i></p><p>Now your NodeJs Socket Server is working perfectly with free ssl from CLoudflare</p><p> </p></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-43601866278479859902021-08-03T23:58:00.003-07:002021-09-12T02:31:55.729-07:00Ubuntu - MySQL Can't set password for root account, even though all command are successful<p>After a fresh install of MySQL or MariaDB on an Ubuntu server, you can run the "<i><span style="color: #cc0000;"><b>mysql</b></span></i>" command line without being asked for your password, even if you have successfully changed the password. While you still cannot access root account from other software like Navicat, php ... the error encountered is "<i>Access denied for user 'root'@'localhost' (using password: YES)</i>"</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgho9mx1QipUhZNlF3woWkno1f5Cu294f7nOoXBcWaQSI0VIWo9OhHdlZZIkEqXbmcs4zgjTXm9e6Hqa7YJ1YydmDXpgmhhWclWXlBXlvHJ8UFZX8gdlsfclN18ayqkihjL6NPTDMmXXD8/s632/Ubuntu-MySQL-MariaDB.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="472" data-original-width="632" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgho9mx1QipUhZNlF3woWkno1f5Cu294f7nOoXBcWaQSI0VIWo9OhHdlZZIkEqXbmcs4zgjTXm9e6Hqa7YJ1YydmDXpgmhhWclWXlBXlvHJ8UFZX8gdlsfclN18ayqkihjL6NPTDMmXXD8/w200-h149/Ubuntu-MySQL-MariaDB.jpg" width="200" /></a></div><h3 style="text-align: left;"><span>What happened ?</span></h3><div>you can run the "<i><span style="color: #cc0000;"><b>mysql</b></span></i>" command without any password because by default, the root account is configured to login via the AUTH_SOCKET plugin. So MySQL Command-Line Client can always connect successfully without being asked for a password while other software can't connect even if the correct password is entered.</div><div><br /></div><h3 style="text-align: left;">Security issue?</h3><div><div>In a way, the mysql root account should only be accessed from the server, and a person who already has root privileges with Ubuntu can obviously optionally reset the mysql root account password. So this default configuration should not be a problem.</div><div>However, if you are a strict person or are familiar with other operating systems like Centos, you will be very uncomfortable with this configuration. If so, bring it back to the same as mysql on Centos by following the steps below.</div><div><br /></div><h3 style="text-align: left;">Solving problems</h3><div>Access the MySQL command-line. </div><div><div></div><blockquote><div>mysql</div><div><div>Welcome to the MySQL monitor. Commands end with ; or \g.</div><div>Your MySQL connection id is 8</div><div>Server version: 8.0.21-0ubuntu20.0 (Ubuntu)</div><div><br /></div><div>Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</div><div><br /></div><div>mysql></div></div></blockquote><div></div><div>Switch to the 'mysql' database</div><div><blockquote>use mysql;</blockquote></div><div>Verify current status</div><div><blockquote>select Host,User,authentication_string,plugin from mysql.user;</blockquote></div><div>You can see: The MySQL ROOT account has no password configured and using the authentication plugin named auth_socket.</div><div>Now configure the ROOT account to use mysql_native_password plugin</div><div><blockquote>ALTER USER root@localhost IDENTIFIED WITH mysql_native_password;</blockquote></div><div>Set new password for root account</div><div><blockquote>ALTER USER root@localhost IDENTIFIED BY 'newpassword';</blockquote></div><div>Flush privileges</div><div></div><blockquote><div>FLUSH PRIVILEGES;</div><div>exit;</div></blockquote><div></div></div><div style="text-align: left;">Done</div><h3 style="text-align: left;">Problem solved !</h3></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-73897234230015840362020-12-29T18:19:00.000-08:002021-09-12T02:31:55.729-07:00Gitlab Error: couldn't deduce an advertise address: no private IP found, explicit advertise addr not provided<p><span style="color: #20124d;">Gitlab log view:</span></p><p></p><blockquote>gitlab-ctl tail</blockquote><p></p><p><span style="color: #20124d;">Something error:</span></p><p>cluster.go:154 component=cluster err="<span style="color: #cc0000;">couldn't deduce an advertise address: no private IP found, explicit advertise addr not provided</span>"</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiMX7rbWxUTDvwDTyC3LsGSsaoMSVBcnMAIO3E8Oa8zCaOokt1fa1RkVcptvtUygZLmsxccmewQes-kJVWHzTpNUirmPUv6zD2q9vq1rp01kU_vP2oqHp6_dYG2rDeDPXHoNfmjgTnFkA/s1000/1_fuvJn5MgjGfMpOcHfvw_MA.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="668" data-original-width="1000" height="165" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiMX7rbWxUTDvwDTyC3LsGSsaoMSVBcnMAIO3E8Oa8zCaOokt1fa1RkVcptvtUygZLmsxccmewQes-kJVWHzTpNUirmPUv6zD2q9vq1rp01kU_vP2oqHp6_dYG2rDeDPXHoNfmjgTnFkA/w247-h165/1_fuvJn5MgjGfMpOcHfvw_MA.png" width="247" /></a></div><p><b><span style="font-size: large;">How to fix !</span></b></p><p>edit file gitlab.rb</p><p></p><blockquote><span style="color: #20124d;">vi /etc/gitlab/gitlab.rb</span></blockquote><p></p><p>Add the following code</p><p></p><blockquote><p><span style="color: #20124d;">alertmanager['flags'] = {</span></p><p><span style="color: #20124d;"> 'cluster.advertise-address' => "127.0.0.1:9093",</span></p><p><span style="color: #20124d;">}</span></p></blockquote><p></p><p>Then</p><p></p><blockquote>gitlab-ctl reconfigure</blockquote><p></p><p></p><blockquote>gitlab-ctl restart</blockquote><p></p><p>Done ! now recheck gitlab log</p><p></p><blockquote>gitlab-ctl tail alertmanager</blockquote><p></p><p>Check alertmanager service</p><p></p><blockquote>netstat -tulpn | grep LISTEN</blockquote><p>output </p><p></p><p></p><blockquote>tcp 0 0 127.0.0.1:9093 0.0.0.0:* LISTEN 30633/alertmanager</blockquote><p></p><div><br /></div>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-7830657759918635336.post-66604499006817067022020-12-28T23:45:00.005-08:002021-09-12T02:31:55.729-07:00Centos 6 - Yum Error: Cannot find a valid baseurl for repo base<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghurboAKGTPC5ghsBcA9gEkKq_o_nFF2dyZYvmtNMfe7UXDlBzSfWTpEd485VUXwzgDJdCMyq5E1ohVje4XnRn4O-5dCW-OznOOnON4Pk4_tKNWcVByECCXHV-0xz85mgBJ0pBBEUnacM/s1200/centos-6+yum.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="794" data-original-width="1200" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghurboAKGTPC5ghsBcA9gEkKq_o_nFF2dyZYvmtNMfe7UXDlBzSfWTpEd485VUXwzgDJdCMyq5E1ohVje4XnRn4O-5dCW-OznOOnON4Pk4_tKNWcVByECCXHV-0xz85mgBJ0pBBEUnacM/w200-h133/centos-6+yum.jpg" width="200" /></a></div><p style="text-align: left;">When using the commands "<span style="color: #990000;"><b>yum install/update</b></span>" on Centos 6 you will get an error:</p><p style="text-align: left;"></p><blockquote style="text-align: left;"><div style="text-align: left;"><span style="color: #20124d;">Loaded plugins: fastestmirror, replace<br />Setting up Update Process<br />Determining fastest mirrors<br />YumRepo Error: All mirror URLs are not using ftp, http[s] or file.<br /> Eg. Invalid release/repo/arch combination/<br />removing mirrorlist with no valid mirrors: /var/cache/yum/x86_64/6/base/mirrorlist.txt<br />Error: Cannot find a valid baseurl for repo: base (base/updates/contrib)</span></div></blockquote><p style="text-align: left;">Reason: Centos 6 is out of date and is no longer officially supported.</p><p style="text-align: left;">Solution: Manual change CentOS-Base.repo</p><p style="text-align: left;"><b><span style="font-size: large;">Step 1:</span></b></p><p style="text-align: left;">Open the following files one by one:</p><div style="text-align: left;"><blockquote><span style="color: #20124d;">/var/cache/yum/x86_64/6/base/mirrorlist.txt<br />/var/cache/yum/x86_64/6/extras/mirrorlist.txt<br />/var/cache/yum/x86_64/6/updates/mirrorlist.txt</span></blockquote></div><p style="text-align: left;">Add the following line at the end of the files:</p><p style="text-align: left;"></p><blockquote style="text-align: left;">https://vault.centos.org/6.10/</blockquote><p style="text-align: left;"></p><p style="text-align: left;"><b><span style="font-size: large;">Step 2:</span></b></p><p style="text-align: left;">Open file "<span style="color: #990000;"><b>CentOS-Base.repo</b></span>" and modify all blocks according to the form below </p><p style="text-align: left;"></p><blockquote>vi /etc/yum.repos.d/Centos-Base.repo</blockquote><p></p><div style="text-align: left;"><blockquote> <span style="color: #20124d;">[base]<br />name=CentOS-$releasever - Base<br />#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra<br />baseurl=http://vault.centos.org/6.10/centosplus/$basearch/<br />gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6<br />gpgcheck=1<br /></span></blockquote><p><span style="color: #20124d;"> </span></p><blockquote><span style="color: #20124d;">#released updates<br />[updates]<br />name=CentOS-$releasever - Base<br />#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra<br />baseurl=http://vault.centos.org/6.10/centosplus/$basearch/<br />gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6<br />gpgcheck=1<br /></span></blockquote><p><span style="color: #20124d;"> </span></p><blockquote><span style="color: #20124d;">#additional packages that may be useful<br />[extras]<br />name=CentOS-$releasever - Base<br />#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra<br />baseurl=http://vault.centos.org/6.10/centosplus/$basearch/<br />gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6<br />gpgcheck=1<br /></span></blockquote><p><span style="color: #20124d;"> </span></p><blockquote><span style="color: #20124d;">#contrib - packages by Centos Users<br />[contrib]<br />name=CentOS-$releasever - Base<br />#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra<br />baseurl=http://vault.centos.org/6.10/centosplus/$basearch/<br />gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6<br />gpgcheck=1</span></blockquote><p><b><span style="font-size: large;">Step 3:</span></b> Done ! Solved !</p><p>Now on you can continue to use <b><span style="color: #990000;">yum</span></b> normally</p></div><p style="text-align: left;"></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-31149418307149036672020-10-06T06:04:00.006-07:002021-09-12T02:22:12.704-07:00Laravel: Force to use HTTPS for all routes and url (Support Cloudflare Flexible SSL)<p>Laravel - In some special cases but especially when you are using Cloudflare Flexible SSL. We need a solution so that all URL on our website (created by url, route function) must be https even though our server currently doesn't support ssl.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0LF6dLDKYG7xb1DjNoMTvhlhdJ2MqBme9gNhDq1DzwTmweYM49nVi7EPOdhIJfyAYf8lQMxCTuZrMssfYPst1KEDoGmpAttIi-WziuMty5Bb4Hmd4H4FfVhG7tPTRvhul-1RRB2sDnPc/s550/Laravel+SSL.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="420" data-original-width="550" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0LF6dLDKYG7xb1DjNoMTvhlhdJ2MqBme9gNhDq1DzwTmweYM49nVi7EPOdhIJfyAYf8lQMxCTuZrMssfYPst1KEDoGmpAttIi-WziuMty5Bb4Hmd4H4FfVhG7tPTRvhul-1RRB2sDnPc/w251-h191/Laravel+SSL.jpg" width="251" /></a></div><p><b>Solution:</b></p><p>Edit <span style="color: #cc0000;">App\Providers\AppServiceProvider.php</span> in the <span style="color: #cc0000;">boot()</span> method</p><p></p><blockquote><div style="text-align: left;"><span style="color: #cc0000;">public function boot()<br />{<br /> </span><span style="color: #38761d;">// custom for cloudflase flexible ssl</span><br /><span style="color: #cc0000;"> if($this->checkHTTPSStatus()){</span><br /><span style="color: #cc0000;"> URL::forceScheme('https');</span><br /><span style="color: #cc0000;"> }</span><br /><span style="color: #cc0000; white-space: pre;"> </span><span style="color: #38761d;">/*...some other code...*/</span><br /><span style="color: #cc0000;">}</span><br /><span style="color: #cc0000;">private function checkHTTPSStatus()</span><br /><span style="color: #cc0000;">{</span><br /><span style="color: #cc0000; white-space: pre;"> </span><span style="color: #38761d;">/*Select code in an option below*/</span><br /><span style="color: #cc0000;">}</span></div></blockquote><p><b>Option 1:</b> Switch to https according to the configuration in the env file</p><p></p><blockquote><div style="text-align: left;"><span style="color: #cc0000;">private function checkHTTPSStatus()<br />{<br /><span style="white-space: pre;"> </span>return env('APP_HTTPS',false) === true;<br />}</span></div></blockquote><p></p><p>In .env file you must to declare APP_HTTPS parameter</p><p></p><blockquote><span style="color: #cc0000;">APP_HTTPS=true</span></blockquote><p></p><p><b>Option 2:</b> Automatically switch to https if the user comes from Cloudflare Flexible SSL</p><p></p><blockquote><div style="text-align: left;"><span style="color: #cc0000;">private function checkHTTPSStatus()<br />{<br /><span style="white-space: pre;"> </span>return (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']==='https');<br />}</span></div></blockquote><p></p><p><b>Option 3:</b> Full SSL</p><p></p><blockquote><div style="text-align: left;"><span style="color: #cc0000;">private function checkHTTPSStatus()<br />{<br /><span style="white-space: pre;"> </span>return !empty($_SERVER['HTTPS']);<br />}</span></div></blockquote><p></p><p><b>Solved !</b></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-30305002039531231532020-08-10T10:21:00.001-07:002021-09-12T02:22:12.704-07:00Laravel Error "$errors is undefined"<div><div><b>Case:</b> In a Laravel project, when you try to get the form validation error object but it fail. </div><div><span></span><blockquote><span> </span><span style="color: #cc0000;">$errors is undefined</span></blockquote><span style="color: #cc0000;"></span></div><div><br /></div><div><b>Reason:</b> Laravel passes the errors variable from the controller to the view file via session. The $errors variable is bound to the view by the <i><span style="color: #990000;"><b>Illuminate\View\Middleware\ShareErrorsFromSession</b></span></i> middleware, which is provided by the web middleware group. So when you do not declare the web middleware group (containing middleware \<b><i><span style="color: #990000;">Illuminate\View\Middleware\ShareErrorsFromSession</span></i></b>) for your route, you will get the error as seen.</div><div><br /></div><div><b>Solution: </b>Check your route to make sure you added <b><span style="color: #990000;">ShareErrorsFromSession</span></b> Middleware</div></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHuAVtvc4u3FLPxw7_BCvkdMqvHf4ej1IG657o9jixsb84G0QhG4W56lEmMRQB1zrakpuYxNZI9mOSq4CdVlq-TSSomqfJN6_8CwkHRRuoBjbMFLAhMoJzJyYoVCr9WVTWS4Ndi-ZntGM/s800/Laravel-Middleware.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="300" data-original-width="800" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHuAVtvc4u3FLPxw7_BCvkdMqvHf4ej1IG657o9jixsb84G0QhG4W56lEmMRQB1zrakpuYxNZI9mOSq4CdVlq-TSSomqfJN6_8CwkHRRuoBjbMFLAhMoJzJyYoVCr9WVTWS4Ndi-ZntGM/w410-h154/Laravel-Middleware.jpeg" width="410" /></a></div><div><br /></div><div><br /></div><div>Solved !</div><div><br /></div><div><br /></div><div></div>Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-7830657759918635336.post-47052623920790552152020-07-26T23:47:00.001-07:002021-09-12T02:22:12.705-07:00Laravel Fix Error : Function name must be a string by Illuminate\Pipeline\Pipeline.php<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfET4iSW5XiDvELTaRbOVZUBxlE9yo1Hm367cMHf5zhoCiHSLwhLpQTatwWTk9BVkYTzyCgMfufaQ-HteIjsXPXn1rePq-VLUwbAlVeOcWJjwBz4Byg8xenJUZzEFv7oXtzjsRV2n_hTc/s512/laravel_icon512.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Laravel" border="0" data-original-height="512" data-original-width="512" height="131" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfET4iSW5XiDvELTaRbOVZUBxlE9yo1Hm367cMHf5zhoCiHSLwhLpQTatwWTk9BVkYTzyCgMfufaQ-HteIjsXPXn1rePq-VLUwbAlVeOcWJjwBz4Byg8xenJUZzEFv7oXtzjsRV2n_hTc/w131-h131/laravel_icon512.png" width="131" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;"><b>Issue:</b> Laravel encountered error "Function name should be a string" or "Function name must be a string" in Api or somewhere. Error reported from file "Illuminate\Pipeline\Pipeline.php"</div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"></div><blockquote><div class="separator" style="clear: both;">$carry = method_exists($pipe, $this->method)</div><div class="separator" style="clear: both;"> ? $pipe->{$this->method}(...$parameters)</div><div class="separator" style="clear: both;"> : $pipe(...$parameters);</div></blockquote><div class="separator" style="clear: both;"></div><div class="separator" style="clear: both;"><b>Reason:</b> You have used some middleware by name that has not been previously declared.</div><div class="separator" style="clear: both;"><b>Debug:</b> Check all middleware used, make sure it is already declared in the file Kernel.php. The most common scenario is that you have not declared 'auth' middleware yet.</div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><div class="separator" style="clear: both;"></div><blockquote><div class="separator" style="clear: both;">protected $routeMiddleware = [</div><div class="separator" style="clear: both;"> 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,</div><div class="separator" style="clear: both;"><span> </span><span> </span>...</div><div class="separator" style="clear: both;">}</div></blockquote><div><b>Done ! </b></div><div class="separator" style="clear: both;"></div></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-64131309795145823372020-07-08T03:46:00.002-07:002020-07-08T03:47:20.848-07:00Fix PHP Mongodb Error: connection refused calling ismaster on 'localhost:27017'<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRNqAk3j3W95ezVjN1ej0BtxdM7xOIrPcnP9XMs-XtOtC25hdVV8fp6vg0SeVu2BxTIeCLPR1DwsoW3ZAjoZ6Cn2r49zP9L8A7UAIN0RhbhmBej-6hujQKXqpAm3NE9ylXCFIKPx7wEvA/s900/Selinux.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="872" data-original-width="900" height="158" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRNqAk3j3W95ezVjN1ej0BtxdM7xOIrPcnP9XMs-XtOtC25hdVV8fp6vg0SeVu2BxTIeCLPR1DwsoW3ZAjoZ6Cn2r49zP9L8A7UAIN0RhbhmBej-6hujQKXqpAm3NE9ylXCFIKPx7wEvA/w164-h158/Selinux.jpg" width="164" /></a></div><div><br /></div><div>Step by step, Debug and fix PHP Mongodb error "No suitable servers found". This tutorial works with pure php and also popular php frameworks today such as: Laravel, Yii, CodeIgniter ...</div><div><br /></div><div><b>Raw Error:</b></div><pre style="text-align: left;">No suitable servers found (`serverSelectionTryOnce` set): <br />[connection refused calling ismaster on 'localhost:27017']<br />[connection refused calling ismaster on '127.0.0.1:27017']</pre><div><b>Debug Step by Step !</b></div><div><b><br /></b></div><div><b>1. Check if mongodb is working or not:</b></div><pre style="text-align: left;">netstat -tulpn | grep LISTEN</pre><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqBanCUHGQq5czXxqB-kyhaZ5GdL7X2XqJWvCQRaZwdTt1jakxDa8FZmhsBisVybjvaNRw7bSdoSF-C-ebOOkeaNyVP21AlVJqHBJwr5UuEuD5pHPw2xXS52ziaNxd5Q-plNJL9vgajvA/s998/mongo.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="82" data-original-width="998" height="41" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqBanCUHGQq5czXxqB-kyhaZ5GdL7X2XqJWvCQRaZwdTt1jakxDa8FZmhsBisVybjvaNRw7bSdoSF-C-ebOOkeaNyVP21AlVJqHBJwr5UuEuD5pHPw2xXS52ziaNxd5Q-plNJL9vgajvA/w500-h41/mongo.jpg" width="500" /></a></div><div>Please check if the mongodb process is working or not, the correct port or not. If the mongodb server is not working properly please resolve that issue.</div><div><br /></div><div><b>2. Check that the php-mongodb extension is installed correctly </b>(Unrelated, but I think it is useful)</div><div>Create a php file with content:</div><pre style="text-align: left;"><?php<br />phpinfo();<br />?></pre><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnX-cRVN_3GP-ybu69WY1dUDZyDB3mIH9ss5VikulyV1YYfL24e3HPrDkwolPX9-F9qlP8azYu4UmAl81J6aA4mPi49eNFwexMbg4656QM4f0J_ixP-wCDpxO1zSYTqP1gWA5JH5DX9Qc/s1185/mongo2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="578" data-original-width="1185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnX-cRVN_3GP-ybu69WY1dUDZyDB3mIH9ss5VikulyV1YYfL24e3HPrDkwolPX9-F9qlP8azYu4UmAl81J6aA4mPi49eNFwexMbg4656QM4f0J_ixP-wCDpxO1zSYTqP1gWA5JH5DX9Qc/s320/mongo2.jpg" width="320" /></a></div><div>If no Mongodb found in the results page means you are missing this ext. Please install it according to the following tutorial: <a href="https://www.codesiri.com/2020/07/install-php-mongodb-extension-for-php7.html">https://www.codesiri.com/2020/07/install-php-mongodb-extension-for-php7.html</a></div><div><br /></div><div><b>3. Can you connect to the mongodb server via the Command line (CLI)?</b></div><pre style="text-align: left;">mongo</pre><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpHwb7LBHSTtYC4VDO-F1OT-xUgpznZ39yWjGS0nYfSPpIukKaQWmItkq3zCb9OT8eCPtAGM8MviF8s5AdIoRKOBj8J9a2uy4VJn0Cojj4Ph-6he_6sOAukmk8wccXFPcwA2Pq52vXXqY/s752/mongo3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="143" data-original-width="752" height="95" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpHwb7LBHSTtYC4VDO-F1OT-xUgpznZ39yWjGS0nYfSPpIukKaQWmItkq3zCb9OT8eCPtAGM8MviF8s5AdIoRKOBj8J9a2uy4VJn0Cojj4Ph-6he_6sOAukmk8wccXFPcwA2Pq52vXXqY/w500-h95/mongo3.jpg" width="500" /></a></div><div><b>4. Check SELinux Permission (Very important)</b></div><div>If you've debugged through steps 1 - 3 and haven't found the problem yet, SELinux is probably the problem that caused your error.</div><div>For added security, SELinux is enabled by default on newer server versions. By default SELinux will not allow apache, PHP is automatically opened to the new socket to connect out (or local). Meanwhile, php-mongodb ext needs to initialize the socket to connect to the mongodb server. So you will encounter the error as seen.</div><div><b><i>To fix this issue grant SELinux permission to apache to freely open the socket.</i></b></div><pre style="text-align: left;">setsebool -P httpd_can_network_connect 1</pre><div><b>Note: </b>Some other php libraries that use sockets may have similar problems with SELinux, for example: php-redis, curl ...</div><div><br /></div><div><b>Done !</b></div>Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-7830657759918635336.post-22123532440002746042020-07-07T10:06:00.001-07:002021-09-12T02:22:12.704-07:00Fix Laravel Error - Please provide a valid cache path<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgS9vbL_EBU6NLwV5Xy0Jbt9LRSFR4sB1BqQxOq4uxiSvEQXj2madlAV8mCtPL9SL9giqhQ0q9wYK4Z4xrKz5agbozPVfeB2q_4exiPc3cGVmGAcAvXN4gEAyMdsUdmUoH-Dd5ILo8wiRE/s400/laravel-logo-1.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="400" data-original-width="400" height="164" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgS9vbL_EBU6NLwV5Xy0Jbt9LRSFR4sB1BqQxOq4uxiSvEQXj2madlAV8mCtPL9SL9giqhQ0q9wYK4Z4xrKz5agbozPVfeB2q_4exiPc3cGVmGAcAvXN4gEAyMdsUdmUoH-Dd5ILo8wiRE/w164-h164/laravel-logo-1.jpg" width="164" /></a></div><div><br /></div><div>Error when install new Laravel project in server.</div><div><br /></div><pre style="text-align: left;">InvalidArgumentException<br />Please provide a valid cache path.</pre><div><br /></div><div>This error can be caused by</div><div><br /></div><pre style="text-align: left;">…/vendor/laravel/framework/src/Illuminate/View/Compilers/Compiler.php line 36</pre><div><br /></div><div><b>* Reason: </b></div><div>To increase performance, the default laravel will need to be configured in a directory to store the cache files of views, sessions ...., so you need a cache directory in your project. Normally this directory will be created automatically, but in some situations such as the user who does not have much experience in the installation of the project git, this cache directory may be lost or incorrect.</div><div><br /></div><div><b>* Solve:</b></div><div>Declare the cache directory for Laravel and make sure php needs write permissions in that directory</div><div><br /></div><div>- create 4 folder for cache:</div><div><br /></div><pre style="text-align: left;">.../bootstrap/cache<br />.../storage/framework<br />.../storage/framework/views<br />.../storage/framework/cache<br />.../storage/framework/sessions</pre><div>(... is the path to your project folder)</div><div><br /></div><div>- grant write permissions to these directories (777 is not a good choice for security)</div><div><br /></div><pre style="text-align: left;">chmod 777 .../bootstrap/cache -R<br />chmod 777 .../storage/framework -R</pre><div><br /></div><div>- To keep these directories empty in the git project, you can add a .gitignore file to those directories with the content:</div><div><br /></div><pre style="text-align: left;">*<br />!.gitignore</pre><div><br /></div><div>- If your server is using selinux remember to give write permission to php</div><div><br /></div><pre style="text-align: left;">sudo chcon -t httpd_sys_rw_content_t .../bootstrap/cache -R<br />sudo chcon -t httpd_sys_rw_content_t .../storage/framework -R</pre><div><b>Done !</b></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-22677637079848745212020-07-04T07:54:00.005-07:002021-09-12T02:31:55.729-07:00[Linux] Install PHP mongodb extension for PHP 7.2 and higherThis article will guide you to install php mongodb extension for PHP7 on linux operating system platform. The operating system we use is Centos, but it is also true for other linux distributions like Ubuntu, Redhat, Fedora ....<br /><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCfr9qSuIx6EDTvtxGVR-ZLofmNWW4fb51bTmI06bxRtRK9Ri_N3TUJu7NEQj4EN_iHmYpWvA5yfNGid_0CLbyUyFe2aBVCSaW_ujmNt06evyKRhyphenhyphenMxHq6BIVPeaZPvRpIFPme8JO-1oU/s480/mongodb-php-driver.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="289" data-original-width="480" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCfr9qSuIx6EDTvtxGVR-ZLofmNWW4fb51bTmI06bxRtRK9Ri_N3TUJu7NEQj4EN_iHmYpWvA5yfNGid_0CLbyUyFe2aBVCSaW_ujmNt06evyKRhyphenhyphenMxHq6BIVPeaZPvRpIFPme8JO-1oU/s320/mongodb-php-driver.jpg" width="320" /></a></div><div class="separator" style="clear: both; text-align: left;">When upgrading to PHP7 we often encounter problems when we cannot install the php-mongo extension. You may encounter the following error message:</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><pre style="clear: both; text-align: left;">Package: php-pecl-mongo-1.6.14-1.el7.remi.5.4.x86_64 (remi)<br />Requires: php(zend-abi) = 20100525-64</pre><div class="separator" style="clear: both;"><b>* Reason: </b></div><div class="separator" style="clear: both;"><div class="separator" style="clear: both;">We have 2 mongo extensions for PHP: php-mongo and php-mongodb. Php-mongo only supports php 5.6, but php-mongodb supports the latest version 7.4. That's why you have an error when trying to install php-mongo for PHP7.</div><div class="separator" style="clear: both;">php-mongo is older and has been discontinued, but it is currently available as a library in remi repos. And php-mongodb is Pecl's latest library but it is not available and you have to install it yourself via php-pear.</div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><b>* Solving problems: </b>install php-mongodb extension for PHP7 </div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><b>Step 1:</b> Install apache(httpd) and php 7</div><div class="separator" style="clear: both;">You can refer to this tutorial: <a href="https://linuxize.com/post/install-php-7-on-centos-7/">https://linuxize.com/post/install-php-7-on-centos-7/</a></div><div class="separator" style="clear: both;"><b>Step 2:</b> Install php-pear</div><pre style="clear: both; text-align: left;">install gcc php-pear php-devel</pre><div class="separator" style="clear: both;"><b>Step 3:</b> Install mongodb extension </div><pre style="clear: both; text-align: left;">pecl install mongodb</pre><div class="separator" style="clear: both;"><b>Step 4:</b> Enable php-mongodb extension </div><div class="separator" style="clear: both;">open php.ini file, then add the following line</div><div class="separator" style="clear: both;"><pre style="clear: both; text-align: left;">;;;;;;;;;;;;;;;;;;;;;;<br />; Dynamic Extensions ;<br />;;;;;;;;;;;;;;;;;;;;;;<br />extension=mongodb</pre><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><b>Step 5:</b> Verify PHP Mongodb extension is enabled</div><pre style="clear: both; text-align: left;">php -m</pre><div class="separator" style="clear: both;"><b>Done !</b></div><div class="separator" style="clear: both;"><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><b>* Note:</b> The syntax used in previous PHP versions ('extension=<ext>.so' and 'extension='php_<ext>.dll') is supported for legacy reasons and may be deprecated in a future PHP major version. So, when it is possible, please move to the new ('extension=<ext>) syntax.</div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><b>* Refer:</b> https://blog.remirepo.net/pages/PECL-extensions-RPM-status</div><div class="separator" style="clear: both;"><br /></div></div></div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><br /></div></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-33881348120649624552020-06-27T09:59:00.003-07:002021-09-12T02:31:55.729-07:00MySQL: Fix Error 2013 - Lost connection to MySQL server during query<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtn2bR2SWLNQKilKGh_w6XIa6_o2am34dknguwpSqS5vOV9iSui5on0Db-sd4f8OsxLkHDvJEOh7181zlV6jnwyUQk1M5dUkvnh9iotFns-LocydssWd4o45NKF8GOBA-Ck16URbSZW9w/s512/mysql.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="512" data-original-width="512" height="205" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtn2bR2SWLNQKilKGh_w6XIa6_o2am34dknguwpSqS5vOV9iSui5on0Db-sd4f8OsxLkHDvJEOh7181zlV6jnwyUQk1M5dUkvnh9iotFns-LocydssWd4o45NKF8GOBA-Ck16URbSZW9w/w205-h205/mysql.jpg" width="205" /></a></div><div class="separator" style="clear: both; text-align: left;"><b>Case:</b> You have successfully connected to a MySQL/MariaDB database. But when trying to import a large SQL file, an error occurred.</div><div class="separator" style="clear: both; text-align: left;"><blockquote>Error Code: 2013. Lost connection to MySQL server during query</blockquote></div><div class="separator" style="clear: both; text-align: left;"><b>Reason:</b></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;">- Timeout due to slow connection or too much data (Not the server's fault)</div><div class="separator" style="clear: both;">- Memory allocated is not enough: the data fields are too large ....</div><div class="separator" style="clear: both;"><br /></div></div><div class="separator" style="clear: both; text-align: left;"><b>Quick fix:</b></div><div class="separator" style="clear: both; text-align: left;">Increase memory for Mysql package.</div><div class="separator" style="clear: both; text-align: left;">Find config file</div><div class="separator" style="clear: both; text-align: left;"><b>Eg:</b></div><div class="separator" style="clear: both; text-align: left;"></div><blockquote><div class="separator" style="clear: both; text-align: left;">Xampp Windows: <i>C:\xampp\mysql\bin\my.ini</i></div><div class="separator" style="clear: both; text-align: left;">Linux: <i>/etc/my.cnf</i></div></blockquote><div class="separator" style="clear: both; text-align: left;"><i></i></div><div class="separator" style="clear: both; text-align: left;">Edit file <i>my.ini (my.cnf)</i>, find <i>[mysqld]</i> block, to change <i>max_allowed_packet </i>to a larger value (16, 32, or 128 M)</div><div class="separator" style="clear: both; text-align: left;"><b>Eg:</b></div><div class="separator" style="clear: both; text-align: left;"><pre style="clear: both; text-align: left;">[mysqld]<br />.....<br />max_allowed_packet=128M</pre><pre style="clear: both; text-align: left;"><b><span style="font-family: inherit;">Then restart MySQL</span></b></pre><div class="separator" style="clear: both;"><b><font color="#d52c1f">*Note:</font> </b></div><div class="separator" style="clear: both;">- If max_allowed_packet is not found, you can add it to your <i>[mysqld]</i> block yourself.</div></div><div class="separator" style="clear: both; text-align: left;">- Increasing memory capacity may affect the stability of MySQL server. So you can freely change it on your local computer, but if you are on the product server, please consider carefully. Another solution to importing large mysql files on the server is to use the command line.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><b>Done ! Solved !</b></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both;"><br /></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7830657759918635336.post-10932628319859163562020-06-22T09:13:00.002-07:002021-09-12T02:22:12.704-07:00[PHP] Laravel Exception: Malformed UTF-8 characters, possibly incorrectly encoded<div>This error can be caused from many laravel modules such as Datatables, Json Output, Monolog ... So this is a very difficult error to analyze and fix. In this article we try to find some way to fix that bug in laravel (in the common cases).</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1M2aaV3Bx23UuwRmlrZ4NRsWw1sRFe-SZzX3Y9iMU_cHl94PzOW6Y9yG_39ZDubRDj8eUEeGuT-js06Cc3xwnq09JU7a6KzA0IykG4X23smsLxh7lvUArs1FXFlfefPNgrWL2T7R4Nt8/s1050/utf8-icons.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="573" data-original-width="1050" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1M2aaV3Bx23UuwRmlrZ4NRsWw1sRFe-SZzX3Y9iMU_cHl94PzOW6Y9yG_39ZDubRDj8eUEeGuT-js06Cc3xwnq09JU7a6KzA0IykG4X23smsLxh7lvUArs1FXFlfefPNgrWL2T7R4Nt8/s320/utf8-icons.jpg" width="320" /></a></div><div><b>Why did you get this error? and how to fix it?</b></div><div><br /></div><div><b><i>* Case 1: In case of failure due to wrong configuration of database connection</i></b></div><div>The most common is when you connect to a second database. Developers often have a configuration that lacks the "charset" and "collation" parameters, causing the error as you see it:</div><pre style="text-align: left;">Exception Message:↵↵Malformed UTF-8 characters, possibly incorrectly encoded</pre><div>To fix it please configure the connection parameters are complete.</div><div><b>Eg:</b></div><div><b><br /></b></div><div><pre style="text-align: left;">'mysql' => array(<br /> 'driver' => 'mysql',<br /> 'host' => 'localhost',<br /> 'database' => 'database_name_1',<br /> 'username' => 'user_1',<br /> 'password' => 'password_1'<br /> <b><font color="#d52c1f">'charset' => 'utf8',<br /> 'collation' => 'utf8_unicode_ci',</font></b><br /> 'prefix' => '',<br /><span style="white-space: pre;"> </span> ....<br /> ),<br />/*mysql2 if you use two database*/<br />'mysql2' => array(<br /> 'driver' => 'mysql',<br /> 'host' => 'hostForDB2',<br /> 'database' => 'database_name_2',<br /> 'username' => 'user_2',<br /> 'password' => 'password_2'<br /> <font color="#d52c1f"><b>'charset' => 'utf8',<br /> 'collation' => 'utf8_unicode_ci',</b></font><br /> 'prefix' => '',<br /><span style="white-space: pre;"> </span> ...<br />),</pre></div><div><br /></div><div><b><i>* Case 2: Regex Error - <span style="background-color: white; font-family: consolas, "courier new", monospace; white-space: pre;">Caused by some </span><font face=""><span style="white-space: pre;">regex function in route, url validate ....</span></font></i></b></div><div><pre style="background-color: white; font-family: consolas, "courier new", monospace; font-size: 14px; line-height: 19px; text-align: left; white-space: pre;">PREG_BAD_UTF8_ERROR 'Malformed UTF-8 characters, possibly incorrectly encoded'</pre></div><div style="background-color: white; font-family: consolas, "courier new", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><br /></div><div style="background-color: white; line-height: 19px; white-space: pre;"><b><i><font face="inherit">*Case 3: JSON Encode, Json output error - Caused by Monolog, Datatables Logs..</font></i></b></div><div style="background-color: white; font-family: consolas, "courier new", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><br /></div><div style="background-color: white; font-family: consolas, "courier new", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><i>To Fix Case 2 & 3 you can try converting the input to utf-8, or remove non-utf-8 characters.</i></div><div><br /></div><div><b>Eg1:</b> Function to auto convert any string to utf-8 encoding</div><div><br /></div><pre style="text-align: left;">$newString = mb_convert_encoding($inputString, "UTF-8", "auto");</pre><div><br /></div><div><b>Eg2:</b> fix utf-8 encoding for Datatables column</div><div><br /></div><div><pre style="text-align: left;">$datatable->editColumn('title', function(Post $node) {<br /> return mb_convert_encoding($node->title,"UTF-8", "auto");<br /> });</pre></div><div><b>Done !</b> For any other questions you can leave them in the comment section at the end of this article. Thank you.</div><div><br /></div>Unknownnoreply@blogger.com0