Thursday, August 15, 2013

Redirect URL with out changing the url in address bar using htaccess

Hi. This is a small trick to redirect the URL to another url with out changing the address bar url. We should write the below code in htaccess file.
RewriteEngine on

RewriteRule (.*)\.com$ $1.php [PT]
RewriteRule (.*)\.in$ $1.php [PT]


This will show an URL to user in the address bar,  but internally it will redirect and execute the code of other URL which we configure here.
After adding this to .htaccess file. check this. 

For example if you want to show url as

http://www.domainname.com/pages/mypage.com

to user and internally we want to call and execute the code of another page like

http://www.domainname.com/pages/mypage.php

then use this sample code in your htaccess file. while executing this, the URL will remain same as 
http://www.domainname.com/pages/mypage.com in address bar, but redirect and executes the content of mypage.php. 
For applying this transfers only to a folder follow this. Then we should write the below code in htaccess file.



RewriteEngine on

RewriteRule ^pages/(.*)\.com /pages/$1.php [PT]
RewriteRule ^pages/(.*)\.in /pages/$1.php [PT]


You can change the pages to your folder. The above code works only when the user requests a page from pages folder. other wise it will ignore this.

mypage.com is redirected to the page of mypage.php and will execute the content of the mypage.php when user type this url in address bar
http://www.domainname.com/pages/mypage.comor 
http://www.domainname.com/pages/mypage.in


Thank you

Monday, July 29, 2013

How to know if the browser is IE in PHP

Hi. This is a small code to check the browser is Internet explorer or not using the PHP.
<?php
if(preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT']))
{
   // The browser is IE 
   // write your code here
}
else
{
   // The browser is other than IE
   // write your code here
} 
?>

If you are executing the above code in Internet explorer browser then if condition will execute. and if you are executing the code in other than Internet explorer then else condition will be executed.

Thank you

Friday, July 12, 2013

How to display the previous days date using PHP

Hi to every one. Today am going to give a small code to show the previous day date using the PHP. Just copy and paste the below code into your editor and change the static number to your number.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d'))));

date('Y/m/d') will gives current date.
strtotime(date('Y/m/d')) will convert the current date to UNIX Timestamp.
Here '-' represents the previous days to show. 
Here 'N' is the number to show 'Nth' days ago date from the current date.
'-N days' will gives the previous 'Nth' day date.
strtotime('-N days',strtotime(date('Y/m/d')) will gives the UNIX Timestamp string of previous 'Nth' day date.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d')))) will display the UNIX Timestamp to date format of YYYY/MM/DD.

For example:
Assume that current date is 2013/07/12. and if we want to display 7 days ago date then write the following code.
date('Y/m/d', strtotime('-7 days',strtotime(date('Y/m/d'))));
It will show the last 7 days ago date in the format of 2013/07/05.


Thursday, June 13, 2013

How to navigate forward and back to the pages using JavaScript

Follow the steps to navigate forward and back to the pages using JavaScript.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.back();
}
</script>
</head>
<body>
<input type='button' name='back' value='Go Back' title='Click here to go back' onClick='goBack()'>
</body>
</html>
When the user click on the button it will go back to last visited page which means previous page.
We can write this code in another way.
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.go(-1);
}
</script>
This script is also works same as the above script. This is the same as the back button functionality of the browser. Here '-1' represents the previous page.

And we can use this script to load the next URL in in the history list.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goForward() {
     window.history.forward();
}
</script>
</head>
<body>
<input type='button' name='forward' value='Go Forward' title='Click here to go Forward' onClick='goForward()'>
</body>
</html>
This is the same as the forward button functionality of the browser.

Thank you...

Wednesday, June 12, 2013

How to do HTTP redirect to a URL using JavaScript

Follow the simple steps to do HTTP redirect to a URL using JavaScript.
<html>
<head>
<script type='text/javascript'>
forwardUrl(); // calling the function to redirect
function forwardUrl() {
     window.location.href = 'http://www.phpboyz.com/page1.php';
}
</script>
</head>
<body></body>
</html>
Change the URL to your url to forward. And call the function to forward.

And we can send the parameters along with the url using this script also.
<script type='text/javascript'>
var value1 = 10;
var value2 = 'testing';
     window.location.href = 'http://www.phpboyz.com/page1.php?param1='+value1+'&param2='+value2;
</script>
This script will forward from current page to requested URL location along with the 2 GET parameters param1 and param2. we can use this 2 parameters where ever we need.
Use $param1 = $_GET['param1'] and $param2 = $_GET['param2'] to get the values in the page1.php.

Thank you...

Friday, April 19, 2013

Navigation menu to show current page link as active - jquery

Hi, now am going to share a small article here, that is how to show current page link as active in navigation  menu using JQuery.
First we need to include JQuery library into the page.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
before that we need to maintain the left menu in separate page and include it the required pages.
assume take a left menu page leftmenu.php
<div id="left_menu_div">
<ul>
<li id="menuitem1" title="menu item 1"><a href="http://localhost/activemenu/page1.php">Page One</a></li>
<li id="menuitem2" title="menu item 2"><a href="http://localhost/activemenu/page2.php">Page Two</a></li>
<li id="menuitem3" title="menu item 3"><a href="http://localhost/activemenu/page3.php">Page Three</a></li>
<li id="menuitem4" title="menu item 4"><a href="http://localhost/activemenu/page4.php">Page Four</a></li>
</ul>
</div>
then add your css in the page
<style type="text/css">
#left_menu_div ul
{
 list-style-type:none;
 font-size:11px;
 line-height:23px;
 border:0px solid #ccc;
}

#left_menu_div ul li {
 margin-bottom:5px;
 outline: 0;
 padding: 3px 3px 3px 6px;
 display: block;
 font-weight: bold;
 border: 1px solid #1c252b;
 border-left:5px solid #ee6e28;
 border-radius: 4px;
 -moz-border-radius: 4px;
 -webkit-border-radius: 4px;
 box-shadow: 1px 1px 1px rgba(0,0,0,0.2); /* CSS3 */
 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.2); /* Firefox */
 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.2); /* Safari, Chrome */
 background:#2a69bf;
 width:180px;
 color:white;
}
#left_menu_div ul li a{
 text-decoration:none;
 font-size:14px;
 color:white;
}

.active {
    background: #0186ba;
 background: -moz-linear-gradient(#04acec,  #0186ba); 
 background: -webkit-gradient(linear, left top, left bottom, from(#04acec), to(#0186ba));
 background: -webkit-linear-gradient(#04acec,  #0186ba);
 background: -o-linear-gradient(#04acec,  #0186ba);
 background: -ms-linear-gradient(#04acec,  #0186ba);
 background: linear-gradient(#04acec,  #0186ba);
 color:red;
 border-radius:0px 3px 3px 0px;
 padding: 3px 0px 3px 5px; 
 margin:-4px -4px -4px -7px;
 width:180px;
}
</style>
and finally include this jquery code in the leftmenu page
<script type="text/javascript">
$(document).ready(function() {
  $currenturl = window.location.href;
  $("#left_menu_div ul li a").each(function() {
   $uurl = $(this).attr('href');
   if($(this).attr('href') == $currenturl){
    $(this).addClass('active');
    }
  });
});
</script>

its over... now you can see the active page link in different than all links.

Thank you...

Monday, April 1, 2013

Find the place name using latitude and longitude in PHP with google geocoder


We can find the place name using google geocoder by giving the Latitude and Longitude.

For example we want to get the place name of the latitude: '17.434545' and longitude: '82.124575' we need to pass this values to the google api to get the place details.

Following code explains how to work with that.

function getPlaceName($latitude, $longitude)
{
   //This below statement is used to send the data to google maps api and get the place
 name in different formats. we need to convert it as required. 
   $geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='
                                         .$latitude.','.$longitude.'&sensor=false');

   
   $output= json_decode($geocode);

   //Here "formatted_address" is used to display the address in a user friendly format.
   echo $output->results[0]->formatted_address;
}


We can use this function in PHP. when we execute the above method the following output will be displayed.

call the function as
<?php getPlaceName(17.434545, 82.124575); ?>

Output : Andhra Pradesh 533436, India

Thank you.