Annunci
RSS

Archivi categoria: Programming

Get User’s Remote IP Address using PHP

A Function in PHP to get user’s remote IP Address

<?php

function getRemoteIP ()
{

// check to see whether the user is behind a proxy – if so,
// we need to use the HTTP_X_FORWARDED_FOR address (assuming it’s available)

if (strlen($_SERVER[“HTTP_X_FORWARDED_FOR”]) > 0) {

// this address has been provided, so we should probably use it

$f = $_SERVER[“HTTP_X_FORWARDED_FOR”];
$reserved = false;

// check reserved range 10.0.0.0 – 10.255.255.255
if (substr($f, 0, 3) == “10.”) {
$reserved = true;
}

// check reserved range 172.16.0.0 – 172.31.255.255
if (substr($f, 0, 4) == “172.” && substr($f, 4, 2) > 15 && substr($f, 4, 2) < 32) {
$reserved = true;
}

// check reserved range 192.168.0.0 – 192.168.255.255
if (substr($f, 0, 8) == “192.168.”) {
$reserved = true;
}

// now we know whether this address is any use or not
if (!$reserved) {
$ip = $f;
}

}

// if we didn’t successfully get an IP address from the above, we’ll have to use
// the one supplied in REMOTE_ADDR

if (!isset($ip)) {
$ip = $_SERVER[“REMOTE_ADDR”];
}

// done!
return $ip;

}

echo “Your ip address”.getRemoteIP();

?>

Annunci
 
Lascia un commento

Pubblicato da su aprile 3, 2013 in Programming

 

Tag: , , ,

Share your Programming Experience

An Invitation to all our followers.
Here you can share your programming experience with us.
So that we can help others to find solutions for their programming problems
you can post in whatever programming language you want.

Thanks
Peter Eskandar

TeamWork

 
2 commenti

Pubblicato da su marzo 26, 2013 in Programming

 

Send Mail using PHP , EasyPHP and Gmail

To run this example you have to :

  • Install EasyPHP
  • Download PHPMailer https://github.com/Synchro/PHPMailer
  • Put the PHPMailer folder inside the PHP installation folder, in my case C:\Program Files\EasyPHP12.1\php\
  • Modify Some parameters in PHP Configuration file (php.ini) 
    • insert this line include_path = “.;${path}\php\phpmailer”
    • set smtp_port = 465
    • SMTP = smtp.gmail.com
    • and uncomment this line extension=php_openssl.dll

After tha Create email.php and put it in your WWW root folder

<?php
require(“phpmailer/class.phpmailer.php”);
$mail = new PHPMailer();
$mail->IsSMTP();

//GMAIL config
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = “ssl”; // sets the prefix to the server
$mail->Host = “smtp.gmail.com”; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = “put ur gmail account here”; // GMAIL username
$mail->Password = “ur email password”; // GMAIL password
//End Gmail

$mail->From = “ur gmail account again”;
$mail->FromName = “name”;
$mail->Subject = “some subject”;
$mail->MsgHTML(“the message”);

//$mail->AddReplyTo(“reply@email.com”,”reply name”);//they answer here, optional
$mail->AddAddress(“destination email address”,”name to”);
$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {//to see if we return a message or a value bolean
echo “Mailer Error: ” . $mail->ErrorInfo;
} else echo “<script>alert(‘Email sent’)</script>”;
// echo “Message sent!”
?>

 Test the page using the following URL : http://localhost:8080/email.php
if you get this message, then you have successfully sent your email

 

 

PHPMail

 

 
Lascia un commento

Pubblicato da su marzo 26, 2013 in Programming

 

Tag: ,

Execute CMD Commands Using a Simple Java Interface

CMDSimulator

 

package cmd;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

 

public class JavaRunCommand extends JFrame {

   private JTextField CMDField = new JTextField(40);
   private JButton ExecuteButton = new JButton(“Execute Command”);
   private JLabel CommandLabel = new JLabel(“Command: “);
   private JTextArea OutPut = new JTextArea(20, 30);
   private JScrollPane jsp = new JScrollPane(OutPut);

   public JavaRunCommand( ) {
   super(“CMD Simulator”);
   Container contentPane = this.getContentPane( );
   contentPane.setLayout(new BorderLayout( ));
   JPanel labels = new JPanel( );
   labels.setLayout(new GridLayout(4, 1));
   labels.add(CommandLabel);

  JPanel fields = new JPanel( );
  fields.setLayout(new GridLayout(4, 1));
  fields.add(CMDField);

  Box north = Box.createHorizontalBox( );
  north.add(labels);
  north.add(fields);
  contentPane.add(north, BorderLayout.NORTH);
  OutPut.setFont(new Font(“Monospaced”, Font.PLAIN, 12));
  contentPane.add(jsp, BorderLayout.CENTER);

  JPanel south = new JPanel( );
  south.setLayout(new FlowLayout(FlowLayout.CENTER));
  south.add(ExecuteButton);
  ExecuteButton.addActionListener(new ActionButton( ));
  contentPane.add(south, BorderLayout.SOUTH);
  this.pack();
}

  class ActionButton implements ActionListener
{

@Override
  public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
  String CMD = CMDField.getText() ;
  try {
  // Run “netsh” Windows command
  Process process = Runtime.getRuntime().exec(CMD);
  String temp = “”;

// Get input streams
  BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

// Read command standard output
  String s;
  System.out.println(“Standard output: “);
  while ((s = stdInput.readLine()) != null) {
  System.out.println(s);
  temp +=s + “\n”;
}
 OutPut.setText(temp);

// Read command errors
  System.out.println(“Standard error: “);
  while ((s = stdError.readLine()) != null) {
  System.out.println(s);
  OutPut.setText(s);
  temp +=s + “\n”;
}
  OutPut.append(temp);
} catch (Exception e) {
e.printStackTrace(System.err);
}

}

}

public static void main(String[] args) {
JavaRunCommand client = new JavaRunCommand();
// Next line requires Java 1.3 or later. I want to set up the
// exit behavior here rather than in the constructor since
// other programs that use this class may not want to exit
// the application when the SMTPClient window closes.
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.show( );
}
}

 
Lascia un commento

Pubblicato da su marzo 25, 2013 in Programming

 

Java SendMail Form :

SendMailProgram

 

package core;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.util.Properties;

import javax.mail.Transport;
import javax.net.ssl.HostnameVerifier;
import javax.swing.JFrame;
public class SMTPClient extends JFrame {
  private JButton sendButton = new JButton(“Send Message”);
  private JLabel fromLabel = new JLabel(“From: “);
  private JLabel toLabel = new JLabel(“To: “); 
  private JLabel hostLabel = new JLabel(“SMTP Server: “);
  private JLabel subjectLabel = new JLabel(“Subject: “);
  private JTextField fromField = new JTextField(40);
  private JTextField toField = new JTextField(40);
  private JTextField hostField = new JTextField(40);
  private JTextField subjectField = new JTextField(40);
  private JTextArea message = new JTextArea(40, 72);
  private JScrollPane jsp = new JScrollPane(message);
  public SMTPClient( ) {
  super(“SMTP Client”);
  Container contentPane = this.getContentPane( );
  contentPane.setLayout(new BorderLayout( ));
  JPanel labels = new JPanel( );
  labels.setLayout(new GridLayout(4, 1));
  labels.add(hostLabel);
  JPanel fields = new JPanel( );
  fields.setLayout(new GridLayout(4, 1));
  String host = System.getProperty(“mail.host”, “smtp.gmail.com”);
  hostField.setText(host);
  hostField.setEnabled(false);
  fields.add(hostField);
  labels.add(toLabel);
  fields.add(toField);
  String from = System.getProperty(“mail.from”, “peterwadid1@gmail.com”);
  fromField.setEnabled(false);
  fromField.setText(from);
  labels.add(fromLabel);
  fields.add(fromField);
  labels.add(subjectLabel);
  fields.add(subjectField);
  Box north = Box.createHorizontalBox( );
  north.add(labels);
  north.add(fields);
  contentPane.add(north, BorderLayout.NORTH);
  message.setFont(new Font(“Monospaced”, Font.PLAIN, 12));
  contentPane.add(jsp, BorderLayout.CENTER);
  JPanel south = new JPanel( );
  south.setLayout(new FlowLayout(FlowLayout.CENTER));
  south.add(sendButton);
  sendButton.addActionListener(new SendAction( ));
  contentPane.add(south, BorderLayout.SOUTH);
  this.pack( );
  this.setResizable(false);
  this.setLocation(200, 30);
}
//The Send Action Class
class SendAction implements ActionListener

{
public void actionPerformed(ActionEvent evt)
   {
   Properties props = new Properties();
   props.put(“mail.smtp.host”, “smtp.gmail.com”);
   props.put(“mail.smtp.socketFactory.port”, “465”);
   props.put(“mail.smtp.socketFactory.class”,
   “javax.net.ssl.SSLSocketFactory”);
   props.put(“mail.smtp.auth”, “true”);
   props.put(“mail.smtp.port”, “465”);

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“Your Gmail Account Here”,”Account Password”);
  }
   });

  try {

    final Message msg = new MimeMessage(session);
   Address to = new InternetAddress(toField.getText( ));
   Address from = new InternetAddress(fromField.getText( ));
   msg.setContent(message.getText( ), “text/plain”);
   msg.setFrom(from);
   msg.setRecipient(Message.RecipientType.TO, to);
   msg.setSubject(subjectField.getText( ));

// This can take a non-trivial amount of time so
// spawn a thread to handle it.

Runnable r = new Runnable( ) {
public void run( ) {
try {
Transport.send(msg);
}
catch (Exception ex) {
ex.printStackTrace( );
}
}
};
Thread t = new Thread(r);
t.start( );
message.setText(“”);
}

catch (Exception ex) {
// I should really bring up a more specific error dialog here.
ex.printStackTrace( );
}

}

    }

     public static void main(String[] args) {
     SMTPClient client = new SMTPClient( );
    // Next line requires Java 1.3 or later. I want to set up the
    // exit behavior here rather than in the constructor since
    // other programs that use this class may not want to exit
    // the application when the SMTPClient window closes.
    client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    client.show( );
    }

}

 

 
Lascia un commento

Pubblicato da su marzo 25, 2013 in Programming

 

Tag: