Usually, viral things are those which spread very quickly. If we take the example of Facebook viral comments, there are things which if a user likes and want to let someone else know, he/she will tag his/her friends there. This keeps going and reaches more users at a very short span of time. If the contents are good, it can lead to a really good reach and user engagement.
Here I am using one of the scripts which I’d created recently. When someone types mynextphone.tk/name in the Facebook comments, it will show a Smartphone name, along with a picture of it. You can see the actual output of the script here http://mynextphone.tk
Creating a simple viral comment script is very simple and easy. Yeah, you can create the scripts in a few minutes and host it on your server. We’ll create a simple script here. We’ll follow this steps:
- We need some static data which will be used to display the actual output when the script is executed
- To format the output, we need to write some good css so that, if a user visits the page by clicking on the link in Facebook comment, it should look good
- Since the script would give random output, instead of any user’s data, we need to redirect all the URLs to home page. E.g. mynextphone.tk/vishal should actually execute home page (index.php). This has to be done through .htaccess
Just 3 steps. Cool, isn’t it? Should we start creating the viral script? Let’s go!
I’m not separating out the different sections of the final script here. You can refer the code at the end of the article. I’ll explain the steps so that you can modify the script as per your requirements.
- For the static data, I’m using multidimensional array (array of array) where each array element will have the name of the phone and corresponding image name
- Next, we have to generate a random number. The number should be between 0 to the size of array-1. E.g. if you have total 10 data in your element, you have to generate a number between 0 and 9. Wondering why so? Well, the array index starts from 0 in programming ( the majority of the languages). By doing so, we will not need to do any other operations with respect to the indexing.
- One we get a random number, we have to fetch the data from the array for that particular index. In this example, we’ll get an array containing the phone name and image name. So we have to again fetch the phone name and image name separately through indexing. So here the index 0 will contain the phone name and 1 will contain the image name.
- After getting the data, we have to display them through some HTML tags on the page. Here I am using
h1tag for the phone’s name and
imgtag to display the phone’s image.
- Once you are done with the data, the final step is to apply some styles to the HTML elements to change the look and feel of the final output. (If you just want to show this contents on the Facebook comments only, and want to redirect your visitor to some other URL then you can skip these stylings)
- Also, we have to redirect all the URLs other than the index, back to index. E.g. mynextphone.tk/name should be redirected to mynextphone.tk. We have to do this via the .htaccess file (Code given below)
You may like: Earn Money Online by doing Easy Micro Jobs
Here is a screen shot of how it looks in Facebook comment
Final Script Code (Save this code as index.php file)
Code to be placed in .htaccess file
And that’s all for now. I’ll be updating this tutorial time to time and based on your inputs too. So please feel free to ask any query/doubt related to the script. I’ll create more scripts and share with you soon 🙂