Quick code for testing

From 5dollarwhitebox.org Media Wiki

Jump to: navigation, search

Contents

PHP

Testing PHP Mailing

I have to write this too often, so here it is so I can copy/paste:

<?php

        $my_addy = "user@domain.com";
        $to_addy = "otheruser@otherdomain.com";
        $subject = "Test Subject";
        $message = "Test Message";

        $headers = "From: $my_addy \r\n";
        $headers .= "Reply-To: $my_addy \r\n";
        $headers .= "Return-Path: $my_addy \r\n";

        mail ($to_addy,$subject,$message,$headers);

?>



File Upload

<?php
# Sample Upload Script

$uploaddir = '/path/to/upload/dir';


if(!$_POST['submit']) {

        print "
           <html>
             <body>
               <form name=\"Uploader\" action=\"$_PHPSELF\" method=\"post\" enctype=\"multipart/form-data\">
                  <input type=\"file\" name=\"upload_file\">
                  <input type=\"submit\" name=\"submit\">
                </form>
              </body>
           </html>
        ";

} else {

        // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
        // of $_FILES.

        $uploadfile = $uploaddir . '/' . basename($_FILES['upload_file']['name']);
        print "Name is " . $_FILES['upload_file']['name'];


        if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $uploadfile)) {
                print "File is valid, and was successfully uploaded.\n";
        } else {
                print "ERROR" . $_FILES['upload_file']['error']; 
        }

        print "<br><br>";
        print 'Here is some more debugging info:';
        print_r($_FILES);


}
?>


Testing PEAR DB

<?php

echo "Connecting to database...<br><br>";

require 'DB.php';

$user = 'user';
$pass = 'password';
$host = 'localhost';
$db_name = 'db_databasename';

$dsn = "mysql://$user:$pass@$host/$db_name";
$db = DB::connect($dsn);

if (DB::isError($db)) {
        print "ERROR";
        die ($db->getMessage());
} else {
        print "Connected successfully to the ${db_name} database using ${user}@${host}";
}

$db->disconnect();
?>



PHP Sessions

<?php

session_start();


if (!session_is_registered('count')) {
   session_register('count');
   $_SESSION['count'] = 1;
} else {
   $_SESSION['count']++;
}
?>

<p>
Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times.
</p>

<p>
To continue, <a href="http://www.example.com/index.php">click
here</a>.
</p>

PERL

Perl Net::SFTP Test

#!/usr/bin/perl -w

use Net::SFTP;
use strict;

my $host = "yourdomain.com";
my $user = "username";
my $password = "password";
my $localfile = "/path/to/testfile";
my %sftpArgs = (user => "$user", password => "$password");
my $sftp = Net::SFTP->new($host, %sftpArgs);

if($sftp->put("$localfile", "/tmp/test_remote_file")) {
        print "The file $localfile was successfully transfered to $host\n";
        exit 0;
} else {
        print "The file transfer failed\n";
        print "sFTP Status: " . $sftp->status . "\n";
        exit 1;
}


Perl DBI Test

#!/usr/bin/perl -w

use strict;
use DBI;
use CGI;

my $cgi = new CGI;



my $dbh = DBI->connect('DBI:mysql:dbname', 'dbuser', 'dbpasswd'
                   ) || die "Could not connect to database: $DBI::errstr";

my $sth = $dbh->prepare('SELECT value FROM sample');
$sth->execute();
my @result = $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect();


print   $cgi->header,
        $cgi->start_html('test'),
        $cgi->h1("Result of query is: $result[0]\n"),
        $cgi->end_html;


Testing Perl Sendmail

#!/usr/bin/perl -w

$mailprog       = "/usr/sbin/sendmail";
$my_addy        = "fromaddy\@domain.com";
$to_addy        = "toaddy\@remotedomain.com";
$subject        = "Testing PERL Mail";
$msg            = "This is my message, booya!";

$headers        = "From: $my_addy" . "\r\n";
$headers        .= "Reply-To: $my_addy" . "\r\n";
$headers        .= "Return-Path: $my_addy" .  "\r\n";
$headers        .= "TO: $to_addy" . "\r\n";



open (MAIL, "|$mailprog -t") || die "Unable to send request\n";
        print MAIL $headers;
        print MAIL "Subject: $subject" . "\r\n";
        print MAIL $msg . "\r\n";;
close(MAIL);
Personal tools