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.
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; | |
} | |
} |
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:
Continue reading »
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:
$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.
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:
foreach($collection2 as $coll) { | |
$collection1->addItem($coll); | |
} |
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!
$xml = simplexml_load_string($xmlstring); | |
$json = json_encode($xml); | |
$arr = json_decode($json,true); |
Happy Coding!
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)