Wednesday 15 January 2014

Redirecting a website to another website without changing the url

In this post Iam explaining how to redirect a website to another website without changing it's url.
For eg: If you have a domain named adidas.com. You want to redirect every page request of adidas.com  to nike.com.
Like adidas.com/aboutus.php should show the contents of nike.com/aboutus.php , but the url will remain as adidas.com/aboutus.php.

So Now I will explain how to acheive this. This can be acheived by CURL function in php.
Before doing this please ensure that you have access to both domains.& curl option is enabled in your server.

Here I will explain with an example.

First make a file named .htaccess

RewriteEngine On
RewriteRule index.html  index.php
RewriteRule Aboutus.html about.php
RewriteRule ContactUs.html  contact.php



Place above code in that .htaccess file.
Then create a file named index.php

<?php
getsite("targetsite/index.php");// Here give the target page from where to show the content

function getsite($url){
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$urlContent = curl_exec($ch);
$excont=str_replace('targetsite','yoursite',$urlContent);// Here replace the menu link with your sites

// Check if any error occured
if(!curl_errno($ch))
{
   $info = curl_getinfo($ch);
   header('Content-type: '.$info['content_type']);
   echo $excont;
}


// close cURL resource, and free up system resources
curl_close($ch);

}
?>

You have to create about.php,contact.php  similar to the above file by just changing the parameter in getsite function.
Hope it works.

Saturday 4 January 2014

Integrating Google map in Websites Where users can Locate their places by a marker

When we create job websites,business directories.etc , situation will araise where we have to make users locate their Location inside a google map. In this modern era it has become an inevitable feautre where everyone uses google map for travelling & locating places.
So In this post I will try to explain how to include that in your php website.
Include this code in your header file :-
 <script type="text/javascript" src="map/jquery-1.4.4.min.js"></script>        
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBDvrqFePHOAvzk1bs2rsg309Pro20FhL0&sensor=false" type="text/javascript"></script>
    <script type="text/javascript" src="map/gmap3.js"></script> 
    <style>
      body{
        text-align:center;
      }
      .gmap3{
        margin: 20px auto;
        border: 1px dashed #C0C0C0;
        width: 500px;
        height: 500px;
      }
    </style>
    
    <script type="text/javascript">
      $(function(){
      
        $("#test").gmap3({
          marker:{
            latLng: [23.00,54.00],
            options:{
              draggable:true
            },
            events:{
              dragend: function(marker){
                $(this).gmap3({
                  getaddress:{
                    latLng:marker.getPosition(),
                    callback:function(results){
                      var map = $(this).gmap3("get"),
                        infowindow = $(this).gmap3({get:"infowindow"}),
                        content = results && results[1] ? results && results[1].formatted_address : "no address";
                      if (infowindow){
                        var lat = marker.getPosition().lat();
                        var lng = marker.getPosition().lng();

                        infowindow.open(map, marker);
                        infowindow.setContent(content+lat+lng);
                        var userdat=content+'&'+lat+'&'+lng;
                      $.post("getmapdata.php", {userdat: userdat}, function(data){
    alert("Google map Location successfully updated: "+data);
});
                      } else {
                        $(this).gmap3({
                          infowindow:{
                            anchor:marker, 
                            options:{content: content}

                          }
                        });
                      }
                    }
                  }
                });
              }
            }
          },
          map:{
            options:{
              zoom: 8
            }
          }
        });
        
      });
    </script>
Here in the above script we can see some included scripts. jquery-1.4.4.min.js can be downloaded from jquery.com site.
Second script included is directly from google source so we didn't have to worry about it.
Third script included is gmap3.js. We have to download it from http://gmap3.net/
Now we have done the basic settings, now it requires only to include map in our page.

<div class="map">
<div id="test" class="gmap3"></div>
   <p> Drag the marker to point to an address</p>
</div>

Now execute the page we can see the map coming in that area.
Now we have to do is to save the latitude ,longitude & location corresponding to the users dragging the mouse.Look at this portion of above code:-
var userdat=content+'&'+lat+'&'+lng;
                      $.post("getmapdata.php", {userdat: userdat}, function(data){
    alert("Google map Location successfully updated: "+data);

Here the data is saved in the getmapdata.php  php file. So our next step is to create that file:-
<?php
session_start();
include("db.php");
if(isset($_SESSION['user_uni'])){
$user=$_SESSION['user_uni'];
}
$fd=$_POST['userdat'];
$fdarr=explode('&',$fd);
$location=$fdarr[0];
$lat=$fdarr[1];
$long=$fdarr[2];
mysql_query("Update tblprofile set location='$location',latitude='$lat',longitude='$long' where login_id='$user'") or die('Error1:'.mysql_error());

?>
Here data will be saved in the table. I have used a demo code, change according to your project. So now  we will get a map similar to this:

Here In above map user have to drag that marker to their location and that location's latitude and longitude will be automatically saved into the database



Thursday 2 January 2014

Redirecting Virtual urls like subdomain names to a subfolder using htaccess file

Here I will explain how to redirect virtual urls similar to subdomain names to a subfolder using .htaccess file.
I will explain it with the help of an example.Iam working on a project named blogger.com.
. Here users can create their own blog urls..  if users name is robert. He can create a url like robert.blogger.com. Here actually we are not creating subdomains manually. If we create so then we have to do it manually for all users.that will go out of control. so what I do is created a folder named user where all requests will execute & it act as a subdomain. Here I wrote this code for htaccess.just try it..


Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.blogger.com
RewriteCond %{HTTP_HOST} ([^.]+)\.blogger.com
RewriteRule ^(.*) user/user.php?username=%1


IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName blogger.com