ad

Monday, 26 October 2015

PHP snippet | Format POST Array

I wrote this little functions to dumb your POST array data into a nice little table. Post associate array can be a little confusing for someone who is just starting out, if ya wanna check your submitted data in formatted table call the function dump_data();

Before:


After:



https://github.com/phpdevsami/Show-post-data-in-a-clean-table

Saturday, 24 October 2015

PHP Script | Store all $_SERVER variable data into database and retrieve


This script will stores all the data that resides in $_SERVER variable into one field in databse which can be retrived as a regular array


How to use:
To use this object instantiate the Server class and pass the required parameters. Required parameters are: ($database_object, $table_name, $field_name). This object offers two method, one to save data to databse, another to retrived saved data.

Requred variables: 
  • $id variable takes the column you are selecting.  
  • $datbase_object takes the databse object that is from database connection.
  • $table_name takes the name of the table that you want to select data from.  
  • $field_name if the name of the field in table where the server information will be or is stored 

Methods:
  • save_data() method will return true or false if data fails to insert into server, like $_Server->save_data();
  • get_data() method will return the exact array that was saved in database, from where you can either loop through it or get each value using its key like, $Server->get_data()['HTTP_HOST'] This method requires and arguement $id 

Storage:
storing server variable in database


 
Retrieval:
php array unserialized






https://github.com/phpdevsami/Store-SERVER-array-into-database
https://dl.dropboxusercontent.com/u/28490184/webdevtown/store%20server%20into%20db.rar

Friday, 16 October 2015

Sending POST requests with AJAX for absolute beginners

First things frst, what is AJAX?
AJAX is a client side script that communicates with server without the need to refresh the web page - in other words it works asynchronously.

Why'd I need AJAX anyways?
What about when you wanna get data from server and put it into a dable without refresh? What about when you wanna check whether username alrady exists without refresh?

How do I send a GET request with AJAX?
Use get method, and a callback function inside that takes two parameters, first returns the page, second the sucess or failed result.
To test our script out, lets create two pages, index.html and data.php, we'll send GET request to data.php from index.html page, and echo whatever is returned. I'd write something in index.php file so I can see whatever has returned.

        $.get("data.php", function(data, text){
            document.write(data);
        } );


Run the above code inside <scirpt> tags and you should see whatever was in the index.php file. That's our basic GET request done.

Q. I'm trying to load a website but it returns 301 page moved permanently?
A. The website has been moved to different URL. Read my HTTP the very basics tutorial to learn more about HTTP and headers. If website has been moved to different URL, it should generally send a Location header to your client. Try that URL.


How do I send POST request to a page?
To send a POST request use the following, pretty much same syntax as GET method but we'll add some POST data, like username and password. Basically we're going to send username and password to data.php page which has following code. Usually you'd get validate POST data and tyr matching it against user and password in SQL DB:
    if( ($_POST["username"] == "cloudcomputing") and ($_POST["password"] == "jquery") ){
        echo "Correct username and password";
    }else{
        echo "Wrong username or password";
    }


Lets send it some data from index.html page. By the way, you can read more about this method here
http://api.jquery.com/jquery.post/

Lets send username and password.
        $.post("data.php", {
            username: "loudcomputing",
            password: "jquery"
        }, function(data, result){
            document.write(data);
        });     
   

Guess what is returns? Wrong username or password. Change sending password to cloudcomputing and it'll grant the access.

So that's pretty much to it, sending basic GET or POST request is where you start and and when you're familiar with it you start adding little things to secure it.. from there on it's all trial and error and basic knowledge of how HTTP protocol works.

Wednesday, 14 October 2015

Can you really learn programming on your own?

The short answer is, yes. Now the long answer..

when most people talk about programming they have this complex algorithm, thousands of lines of code, weird looking flocharts in mind, and more often than not this raises a questions whether such complex subject is even possible to master without attending college, taking computer science program or being taught from some guru

I get dozens of questions from people asking how to learn programming? what's the best resource to learn programming? and so on.. I ask these guys, why would you like to learn programming? they say, I want to build website, then I ask, what website?, I get a response something along the lines that they want to be able to build any website. Right there I know even if these people started programming right now and came back after 5 years, they would still be wondering around, learning random things from random videos, making random small programs.

Now here is another guy. He wants to build a page to collect email addresses for marketing purpose. Right there half of his learning is done.. this guy is far more likely to understand and learn programming than the other dude who is still lost.. not to mention both of these are amateurs, except one has a vision of what he wants to do, is well aware of his ultimate goal, while other guy is looking for something that may along the way define his ultimate goal.

Another, difference between this guy and the previous guy is, this guy right here will not bother clicking on links like Can you really learn programming on your own? he knows what exactly he wants, all he needs is a path to get there. Instead the question he may ask is, how to build this.

To answer the title of this post Can you really learn programming on your own? I will raise a counter question, what is it exactly that you would like to build after you know programming? when you have answered this question, half of your puzzle will be solved, and to solve another half, I am going to link you to this guy.

Now lets do a little time travelling.. theory or techniques you may have missed from not taking CS course, will come and block your way down the road and force you to learn. It does not really matter whether you learn them now or later on, fact of the matter is, as you get better and better, you will be forced to learn it all, your goals will make you do it, now it will become your own choice whether you want to be taught or you can figure things out to acomlpish your goals yourself.

Sunday, 11 October 2015

How to verify data integrity using MD5 cryptographic algorithm

What is MD5 you ask? 

MD5 stands for message digest 5. It's an algorithm used to create data digest to verify it's integrity. Just like every human being has unique fingerprint, every file that goes through this algorithm brings a fixed length hash, this hash cannot be reversed.

What's the use of MD5?

MD5 is most commonly used to hash user password and data verification. When user signs up to a website his password is hashed, and stored in the database, when user tries to login, his password is again hashed and matched against the hash stored in database. So even if his website database is compromised, hacker wouldn't be able to see the passwords, thought this doesn't mean your data is completely secure, more often than not users use common passwords like 123456, 00000 etc.. hacker would simple use a dictinary of common known hashes to crack down your password, so it's very important that your suggest user to choose as strong password as possible.

How does data verification work with MD5?

Data verification or data integrity check work by creating a digest or hash of a certain application/file, every digest generated by MD5 is unique. For example, lets say you've made a file avilable for download, how would the users know whether someone who's linking to a mirrored download is a genuine file and not some virus attached to original file? Well that's where MD5 comes into play, you can compute an hash of your application and post it on your website, now whenever someone downloads it from your or someone else's website he would use a utility like Microsoft file checksum integrity verifier to compute a hash for either MD5 or SHA1, and compare it against the hash that was provided on your original website, if the hash is same, the file's exact as the original one, else, something in that file has been modified.