Thursday, October 2, 2014

Best way to get your client/visitor IP address using PHP

The simplest way to get the visitor’s/client’s IP address is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables.
However, sometimes this does not return the correct IP address of the visitor, so we can use some other server variables to get the IP address.
The below both functions are equivalent with the difference only in how and from where the values are retrieved.
getenv() is used to get the value of an environment variable in PHP.

// Function to get the website visitor IP address
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
$_SERVER is an array that contains server variables created by the web server.
// Function to get the website visitor IP address
function get_client_ip() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

Monday, September 1, 2014

Install PHPMailer on Windows Server

PHPMailer is simple enough to use but in windows server you may face problem.because it is not same as we do.Check the following steps

1.  Make a new folder 'includes' in  your PHP installation folder.
    Example  (c:\php\includes)

2. You just need to upload PHPMailer class folder  inside the 'includes' folder.
     Example C:\php\includes\PHPMailer

3. Add this include path in your php.ini . commencement sendmail_path and use the path
   Example  sendmail_path ="C:\php\includes\PHPMailer\HPMailerAutoload.php"

So in php.ini file it will look like

[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = localhost
; http://php.net/smtp-port
;smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="C:\php-5.5.14\include\PHPMailer\HPMailerAutoload.php"

4. Also keep PHPMailer class folder  in your root directory. use the class
   Example
               require 'class/PHPMailer/PHPMailerAutoload.php';

$from       = "test@test.com";
$mail       = new PHPMailer();
$mail->IsSMTP(true);            // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com"; // SMTP host here use gmail
$mail->Port       =  465;                    // set the SMTP port
$mail->Username   = "test@gmail.com";  // SMTP  username
$mail->Password   = 'ur gmail pass';  // SMTP password
$mail->SMTPSecure = 'ssl';
$mail->SetFrom($from, 'Support Test');
$mail->AddReplyTo($from,'Support Test');
$mail->Subject    = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($to, $to_name);
if($mail->Send())
{
                     echo "mail sent!!";
                 }
                else  echo "mail not sent!!";

Thursday, August 21, 2014

Copy text/data from id/class/selector and past another place jquery

Copy anything from one class /div/id/element and past in other via jquery.
Example

<html>
<body>
<div id="copyID">
Hello sharif
</div>

<div id="pastID">

<div>

<script>
$(document).ready(function() {
    $("#copyID").click(function() {
        $("#pastID").html($(this).html());
    });
});
 </script>
</body>
</html>

Now Hello sharif will show in "pastID" div.
Thanks

Total Pageviews