Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday 4 November 2017

How to append GET parameters in url PHP

First make one function like below

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$param = GET;

function filter_url($url,$param){

    $parsed = parse_url($url);
    $query = str_replace("&","&",$parsed['query']);
    parse_str($query,$params);
    unset($params[$param]);
    $new_query = http_build_query($params);
    $newUrl = $parsed['scheme']."://".$parsed['host'].$parsed['path'];
    if($new_query){
        $newUrl .= "?".$new_query;
    }
    return $newUrl;

}

How do i reverse parse_url in PHP


$parsed = parse_url($url);

You can reverse back parse url like below

$newUrl = $parsed['scheme']."://".$parsed['host'].$parsed['path'];
$new_query = http_build_query($params);
if($new_query){
    $newUrl .= "?".$new_query;
}

Sunday 28 August 2016

How to fetch all files name from folder in PHP


Fetch all files name from folder in PHP

    if ($handle = opendir("C:\wamp\www\yoursite/download/")) {

            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != "..") {
                   
                        echo "<b>".preg_replace('/\\.[^.\\s]{3,4}$/', '', $entry)."</b>";
                   
                }
            }

        closedir($handle);
    }

Thats it.... Enjoy Chandresh rana's Coding... :)

Friday 27 May 2016

Download image from url and save on my server using PHP


Download image from url and save on my server using php

Kindly Use below code to download image and store it to your own server.

//Get the file
$url = "http://ranachandresh.blogspot.in/chandresh.jpg";  //test url you can use your own url
$content = file_get_contents($url);

//Store in media.
$imageInfo = pathinfo($url);
$imageName = $imageInfo['basename'];
$fp = fopen("../media/import/".$imageName, "w");  // You can use your own path here
fwrite($fp, $content);
fclose($fp);


That's it. Enjoy Chandresh rana Coding...

Wednesday 27 January 2016

Call to undefined function mb_convert_encoding() in php


Sometimes people receiving this kind of error : Fatal error: Call to undefined function mb_convert_encoding() in /public_html/this/this.php at line 188. Normally this kind of errors comes in PHP Sites and PHP framework aswell.

It looks like PHP mbstring not installed on your server.

Solution :

In my case I have just uncomment ;extension=php_mbstring.dll in php.ini file and issue has been resolved.

Don't forget to restart apache server after uncomment ;extension=php_mbstring.dll

Monday 26 October 2015

How to Redirect non-www url to www url with .htaccess PHP


How to Redirect non-www url to www url with .htaccess PHP

write below code in your .htaccess file at very top....

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Friday 25 September 2015

How to create random string in PHP


How to create random string in PHP

Sometimes people wants random name like image name, Personal name, book name, etc.... Here i give you short example of random string.

<?php

$char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
$length = 10;
for ($i = 0; $i < $length; $i++) {
    $randomString .= $char[rand(0, strlen($char) - 1)];
}

echo $randomString;
?>

Wednesday 27 May 2015

Previous page link in php


Previous page link in php

$ref = getenv("HTTP_REFERER");
session_start();
if($ref != ''){
$_SESSION['ref'] = $ref;
}
<a href="echo $_SESSION['ref'];">BACK</a>0

Wednesday 8 April 2015

How to Get the Current Page URL in PHP

How to Get the Current Page URL in PHP

 $CurrentURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$CurrentURL .= "s";}
 $CurrentURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $CurrentURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $CurrentURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }

 echo $CurrentURL;

Tuesday 23 December 2014

Check string exist in another string php


Check string exist in another string php

You can check this using the strpos() function. Check below example.

$string = 'Dress material with Standard Stitching';
if(strpos($string,'Dress material') !== false){
    echo 'Condition true';
}

Tuesday 4 November 2014

How to get referrer URL in PHP


How to get referrer URL in PHP

Referrer show you where to user come on our site. In other words we can say last url of browser is Referrer URL.

<?php
$ref = getenv("HTTP_REFERER");
echo $ref;
?>
<?php
    if($_SESSION['refurl']==null)
    {
        $ref = getenv("HTTP_REFERER");
        $_SESSION['refurl']=$ref;
        echo $ref;
    }
    else
    {
   echo $_SESSION['refurl'];
    }
?>