Posts marked with "PHP" in categories

Encode/Decode URL in Javascript and PHP

URL encoding and decoding can be done in both Javascript and PHP. In this post I’m going to tell you the functions used in both languages for url encoding/decoding.
Javascript
 Encoding

encodeURIComponent(url);

Decoding

decodeURIComponent(url);

PHP
 Encoding

urlencode(url);

Decoding

... [READ MORE]

Check whether request is made from Ajax – PHP

To check whether a request has been made is from Ajax or not you can use $_SERVER[‘HTTP_X_REQUESTED_WITH’]. See the following example:

if(strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’])==’xmlhttprequest’){
 echo "The request is from an Ajax Call";
}else{
 echo "The request is not from an Ajax Call";
}
?>

There you go. You now know how to check whether request made to file is from an Ajax Call or not in PHP.

... [READ MORE]

Add GET or POST parameters on include files in PHP

If you want to add GET parameters or POST parameters to files loading using include function in PHP then you should know the following:

It’s not possible. But it is in a different way.

If you are adding GET parameters to file name like the following, then it won’t work:

The above code won’t load the file, because PHP Include function only looks for files, it won’t send any parameters. It will search for file.php?user=subin in the directory for the case above. You can make it work in a different way.

... [READ MORE]

How to execute a PHP PDO prepared statement

PDO – a method used to execute SQL statements risk free. Fast, secure and easy when compared to mysqli and mysql functions in PHP.
A lot of newbies including me heard about PDO mostly in the 2010 – 2013 years when a lot of sites got hacked of Mysql Injection. PDO is the easiest and safest way to execute an SQL code, complete risk free.
In this post I’m gonna tell you how to execute a prepared statement in PDO and the common mistakes made by programmers which results an error while executing SQL code.

... [READ MORE]

How to redirect users to a page in PHP

If you want to redirect users to a specific page or url like when user is not logged in or something in PHP then you can accomplish this with header function or by Javascript printed by echo function.
The following example will redirect user to http://subins.hp.af.cm

Or you can use the normal Javascript :

window.location=’http://subins.hp.af.cm’;"; ?>

What’s the difference ?

header function is called in server itself not when the page reaches the browser. So no content will load, instead the client’s page will be redirected to the specified url.

... [READ MORE]

Facebook Hacker Cup 2013:Beautiful Strings solution in PHP

Here is the solution for FHC 2013 Beautiful Strings problem in PHP:

$ip = fopen(‘input.txt‘, "r");
$test_cases = trim(fgets($ip));
    $c=1;
    while($c!= $test_cases+1){
        $case=preg_replace(‘/[^a-z]+/’, ", strtolower(trim(fgets($ip))));
        $vals=array_count_values(str_split($case));
        arsort($vals);
        $beauty=26;
        $max_b=0;
        foreach($vals as $v){
            $max_b +=$v*$beauty;
            $beauty–;
        }
        print("Case #".$c.": ".$max_b);
        if($c < $test_cases){
            print("
");
        }
        $c++;
    }

... [READ MORE]

Check whether mysqli connection is made or not in php

This function helps to check whether connection the mysql server has been made or not.

Here is the code:

if ($mysqli->ping()) {
echo ("Our connection is ok!n");
} else {  

    printf ("Error: %sn"$mysqli->error);
}

The $mysqli variable is a connection made via mysqli function.
Know more about mysqli ping function here.
... [READ MORE]

Hide File Extensions In Browsers Using .htaccess

Many of us programmers want to hide the file extension of server files such as .php, .jsp, .py etc….

You can easily hide these extensions with the help of a .htaccess file. This feature is only available to Apache servers.
Create a file named .htaccess if your root directory of site doesn’t have one.
Add these lines to the .htaccess file :
<span style="background-color: white; font-family: inherit;"><span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteEngine</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> </span><span class="kw2" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">on</span></span>
<span style="background-color: white; font-family: inherit;"><span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME} !-d</span></span>
<span style="background-color: white; font-family: inherit;"><span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.php -f</span></span>
<span style="background-color: white; font-family: inherit;"><span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteRule</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> .* $0.php</span></span>
The above lines wll hide the PHP extension. If you want to hide the HTML and other extension too add these line after 
<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.php -f</span>
as you want. You can also replace it if you don’t want php extensions to hide.

HTML

<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.html -f</span>

JSP

<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.jsp -f</span>

ASP

<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.asp -f</span>

PY

<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.py -f</span>

JS

<span class="kw1" style="border: 0px; color: #2060a0; line-height: 18.200000762939453px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap;">RewriteCond</span><span style="line-height: 18.200000762939453px; white-space: nowrap;"> %{REQUEST_FILENAME}.js -f</span>
You really should go into each page and delete the extensions, but I believe you can redirect them by adding another rule :
<span style="color: #2060a0;">RewriteRule </span>(.*).<span style="color: red;">html</span> $1 [R=301,NC]
The above rule will redirect all .html pages to non .html extension page.
Replace the red highlighted text with any extension you want to redirect.
... [READ MORE]

Create Facebook Like System With jQuery, MySQL, PDO In PHP

Facebook have it’s own like system which pretty good. This post will tell you how to create a "Like system" like that of Facebook. All you have to need is jQueryMySQL Database and PDO PHP extension. This technic is partially used in my social network, Open. See the demo there in the social network. But you have to signup.

You should create a user table with users data including user idusernamepasswordemail etc…
<div>
  <span style="color: red; font-family: inherit;">The table should be like this:</span>
</div>
Let’s start by creating a likes table where we store the like actions by the user.

... [READ MORE]

Friendshood Update new feature – Post to Facebook

I updated Friendshood. The new feature is that the user can post status update and photos to Facebook along with posting in Friendshood. I implemented this function using Facebook PHP SDK. You can download the SDK here.

It was not quite easy. It was hard to make Picture uploading function along with the normal status update. Next I’m gonna implement twitter posting from Friendshood.

I’m gonna tell you how to post a status update in the following posts. Wait for it.

... [READ MORE]

Follow/Subscribe

Telegram 

Mastodon  Twitter

GitHub GitLab

Subdomains

Demos  Lab

Past

This blog was once on WordPress. Now a static site. See source code on

GitLab