Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, May 13, 2010

PHP Sockets Loop

$i=0;

$jsonPing = "{\"c\":\"ping\",\"d\":{}}\0";

while (1) {

$i++;

if($i == 1000) {
fwrite($socket,$jsonPing);
$i=0;
}

$data = "";
$data .= @fread($socket, 4096);
$data = str_replace("\x1f", " ", $data);
$data = str_replace("part", " part", $data);
$data = str_replace("join", " join", $data);
$data = str_replace("said", " said: ", $data);
$data = trim($data);

$jsonData = json_decode($data,true);

if ($jsonData['from'] == "ayt") {
$data="";
flush();

}else{
if( ($jsonData['u']) && ($jsonData['t']) && (strpos($data, "said")) ) {
echo $jsonData['u']." : ".str_replace($jsonData['u'], "", $jsonData['t'])."
\n";
flush();
ob_flush(); }


}}

fclose($socket);

Monday, May 10, 2010

Check to see which server is which (XML output)

header("Content-type: text/xml");

$getBeta = `/usr/bin/dig beta.sampledomain.com A +short`;
$getBeta = trim($getBeta);

$amIBeta = $_SERVER[SERVER_ADDR];
$amIBeta = trim($amIBeta);

if ($getBeta == $amIBeta) {
$IAmBeta = "true";
}else{
$IAmBeta = "false"; }

header.php

Quick header file to use include or require with in PHP:

$mysql_user="mysqluser";
$mysql_host="mysql.sample.com";
$mysql_db="mydb";
$mysql_pass = "mysqlpassword";

mysql_connect($mysql_host,$mysql_user,$mysql_pass);
@mysql_select_db($mysql_db) or die("Unable to connect to database...");

foreach ($_REQUEST as $key => $value) {
$_REQUEST[$key] = mysql_real_escape_string($value);
}

Wednesday, May 5, 2010

Post Vars as an Array

$result_storeList=mysql_query('SELECT * FROM stores');

while($arrayStoreName=mysql_fetch_array($result_storeList)) {
echo "".$arrayStoreName['name']."(put html checkbox here with $arrayStoreName['ID']).(other HTML here)\n";
}


Submit that to:

$getStore = "SELECT * from stores";
$getStoreQuery = mysql_query($getStore);
while($getStoreArray = mysql_fetch_array($getStoreQuery)) {
        $totalStoreNum = $getStoreArray['ID'];

if(htmlspecialchars($_POST[$getStoreArray['ID']])) {

$storeChoice = $getStoreArray['ID'];

$sql_Query = "INSERT INTO storeItems (storeID,itemID) VALUES ('".$storeChoice."','".$recordID."')";

mysql_query($sql_Query); }

}

File Uploading

// Where the file is going to be placed
$target_path = "/tmp/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['itemUpload']['name']);

if(move_uploaded_file($_FILES['itemUpload']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['itemUpload']['name']).
    " has been uploaded
\n";
} else{
    echo "There was an error uploading the file, please try again!";
}

$localFile = "/tmp/".basename( $_FILES['itemUpload']['name']);

$remoteFile = "/var/www/push/".basename( $_FILES['itemUpload']['name']);

$scp_connection = ssh2_connect('remotehost',22);
ssh2_auth_password($scp_connection, 'username', 'password');

ssh2_scp_send($scp_connection, $localFile, $remoteFile , 0664);

Upload a file via HTTP, then SCP it to another server.  Been using the hell out of this so I can "sanitize" what files get uploaded to the production server (and force them to push it to Dev first).

Wednesday, April 28, 2010

PHP Authentication Vars

Just a quick one for me to remember:

$_SERVER['PHP_AUTH_USER']
$_SERVER['PHP_AUTH_PW']

Get current auth info.  Going to be using this in later code.

PHP and JSON

PHP's json_decode function works well, unless, of course, you get the JSON from a socket server and forget to trim it.  This kicked my butt for a while yesterday.

Another important one to remember is var_dump (which kept showing up as NULL with json_decode before I started trimming the input).

Monday, April 19, 2010

Something to do with a PHP Directory Listing (Make it an HTML Option on another server)

$fp = fopen('http://whereever/whoever.php','r');

$content = '';

while($line = fgets($fp,4096)) {

        $content = $line;
        echo "\n".$content."\n\n"; }

Quickie PHP Directory Listing

$path = ".";

$dir_handle = @opendir($path) or die("Cannot open directory");

while ($file = readdir($dir_handle)) {

if (($file == ".") || ($file == "..") || ($file == "dropdown.php") || ($file == ".svn") || ($file == "old")) {

}else{
echo $file."\n"; }

}

Thursday, April 8, 2010

Quickie PHP Socket connection

$config = array(

'server' => 'serverName',
'port' => 'portNo' );

$socket = fsockopen($config['server'], $config['port'], $errno, $errstr);

if(!$socket) {

        die("Error on page: ".$errno." ".$errstr); }
//uncomment below if stream blocking is needed.
//stream_set_blocking($socket, 1);
$data .= fread($socket, 4096);

echo $data."\n";

fclose($socket);

Friday, April 2, 2010

PHP Code for XML

At my current job I'm writing a lot of PHP scripts to take the load off of our programmers and pass that load to our art dept. Much of the data gets passed as static XML files, which I'm converting to dynamic XML from MySQL via PHP, using the following code:

?php

//The big one to make PHP spit it out as XML

header("Content-type: text/xml");

$mysql_user="dbUser";
$mysql_pass="dbPass";
$mysql_host="dbHost";
$mysql_db="dbDB";
mysql_connect($mysql_host,$mysql_user,$mysql_pass);

mysql_select_db($mysql_db);

$sql_query="SELECT * FROM configTable";

$sql_result=mysql_query($sql_query);


Not the cleanest code, I know, but it works.