Browsing articles in "PHP"
Jun 12, 2013
kalpesh

PHP redirect without header()

It’s very frustrating when you want to redirect from one page to another, and you get the error:

Warning: Cannot modify header information – headers already sent by (output started at /some/filename.php:12) in /some/filename.php on line 99

The problem is Headers are already sent, or in simple language the output is already sent to browser when your header() function is called. The best solution is not to pass headers (send html/output to browser) till the point header() function is fired.

Another way is to use output buffering. Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. For using this method, you have to start your file with ob_start(); so that output gets buffered.

But what if you don’t care whether the output is sent or not, you anyway want the redirect to work? This simple function will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function redirect($url)
{
    if (!headers_sent())
    {
        header('Location: '.$url);
        exit;
    }
    else
    {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>'; 
        exit;
    }
}
Nov 2, 2012
kalpesh

Buy: Pinterest Auto Pin images right from your website!

Pinterest has yet not released their API which can help developers to integrate to the system and autopin images directly from their website/blog. Yet, I have created an Pinterest API in PHP which will post your image to Pinterest with message, link and price.

Just after entering correct email and password of your pinterest account, and providing Image URL, Message and link URL, you will be shown the list of boards you have, so that you can select any one and pin the image on it right from your website! Sounds interesting!? Here is the image that will help you understand how it works:

Pinterest Auto Post image API


Continue reading »

Oct 17, 2012
kalpesh

PHP: is_int() vs is_numeric()

Both of the functions looks similar, but there is a difference, which can screw your time if you’re not aware of it and using blindly! is_int() seems same to is_numeric(), checking the variable if it’s integer or not, but it’s not exactly what you’re thinking.

The key difference between these two functions is that is_int() checks the type of variable, while is_numeric() checks the value of the variable.

From PHP.net,
is_int: Find whether the type of a variable is integer
is_numeric: Finds whether a variable is a number or a numeric string

So, if you check something like:

1
$var = "123";

$var is a string of numbers, not an integer value.
Therefor is_int should return false as it’s not an integer, it’s a string.
However it is a numeric string, so hence is_numeric should return true.

Jul 29, 2012
kalpesh

Magento, PHP: Merging/Joining two objects collections

It’s easy to merge two arrays with array_merge, but have you came across to merge two objects? It’s not that easy in Magento. You need to convert it to array first, merge them, and convert it to the object again to make it work. If you have two objects of different class, then it’s really difficult job to merge as both will not be compatible. If they are from similar classes or share same elements inside, then it’s not that tricky.

If you want to add collection2 in collection1 (in Magento), where collection1 will be a merged form of both, you can do so by:

1
2
3
foreach($collection2 as $coll) {
    $collection1->addItem($coll);
}

Continue reading »

Jul 26, 2012
kalpesh

Convert PHP XML to JSON, XML to Array, JSON to Array

Converting PHP XML to JSON, XML to Array, JSON to Array is very difficult task for some people. But, in PHP, believe me it’s very easy. One line of code each! Don’t believe? Just check out the code below and test it yourself!

1
2
3
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$arr = json_decode($json,true);

Happy Coding!

Pages:12»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications