Accronyms to know

PEAR:
PHP Extension and Application Repository

Web Services
SOAP:
Simple Object Access Protocol
REST:
Representational State Transfer

Posted on 27 Oct 2009, 19:37 - Category: Notes
No comments - Edit - Delete


Stop visitor from hitting AJAX loaded PHP content page directly $_SERVER['REQUEST_METHOD']

This was a real hunt.
Built a framework.
Load everything into it with AJAX.
Don't want visitors to be able to hit PHP content pages on their own.

$_SESSION variables work, but only on first hit.
If, for example, they came from a Google search result, it would force them into the framework,
but if they hit Back and then selected another result,
it wouldn't!

Global variables work for included php file content,
but are not accessible from php code loaded in via AJAX.

$_SESSION["PHP_SELF"] for the loaded content php page is the same with or without the framework,
it's simply that script's location on the server

Finally!
$_SESSION['REQUEST_METHOD'] seems to do the job.

if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
// echo "loaded inside the framework, all clear";
// this is because I SET the AJAX functions to POST!
}
else
{
// forward into the framework based
// based on $url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
// see forwarding post
// url method here is GET because page outside the framework is simple index.php?id=12 or similar!
// but logic works even if /another_page.php is
// loaded because the METHOD isn't POST

header("HTTP/1.1 301 Moved Permanently");
header("Location: $sendto");
}

Important Note:
This logic that checks METHOD is included in ALL pages dynamically loaded, but NOT initially loaded by the index.php/framework

keywords: POST, AJAX, PHP, php, Php, $_SERVER, SERVER, GET, framework, method, REQUEST_METHOD, request_method, server

Posted on 27 Oct 2009, 12:56 - Category: AJAX
No comments - Edit - Delete


AJAX photo upload

// index
// don't forget the xmlhttp.js (also posted in this blog)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="xmlhttp.js"></script>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<div id="showimg"></div>
<form id="uploadform" action="process_upload.php" method="post" enctype="multipart/form-data" target="uploadframe">
Upload a File:<br />
<input type="file" id="myfile" name="myfile" />
<input type="submit" value="Submit" onclick="uploadimg(document.getElementById('uploadform')); return false;" />
<iframe id="uploadframe" name="uploadframe" src="process_upload.php" class="noshow"></iframe>
</form>
</body>
</html>

//process_upload.php

<?php
//process_upload.php

//Allowed file mime types.
$allowedtypes = array ("image/jpeg","image/pjpeg","image/png","image/gif");
//Where we want to save the file to.
$savefolder = "images";

/*
//First, kill off all older files.
if (is_dir ($savefolder)){
$scanarray = scandir ($savefolder);
$numdirs = count ($scanarray);
//parse the array
for ($i = 0; $i < $numdirs; $i++){
// make sure it is not the '.' or '..' files
if ($scanarray[$i] != "." && $scanarray[$i] !="..") {
//make sure it's a file
if (is_file ($savefolder . "/" . $scanarray[$i])){
unlink ($savefolder . "/" . $scanarray[$i]);
}
}
}
}
*/

//If we have a valid file.
if (isset ($_FILES['myfile'])){
//Then we need to confirm it is of a file type we want.
if (in_array ($_FILES['myfile']['type'],$allowedtypes)){
//Then we can perform the copy.
if ($_FILES['myfile']['error'] == 0){
$thefile = $savefolder . "/" . $_FILES['myfile']['name'];
if (!move_uploaded_file ($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error uploading the file.";
} else {
//Signal the parent to load the image.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<img src="<?=$thefile?>" onload="doneloading (parent,'<?=$thefile?>')" />
</body>
</html>
<?php
}
}
}
}
?>

// other functions to save into functions.js

//functions.js

//Function to determine when the process_upload.php file has finished executing.
function doneloading(theframe,thefile){
var theloc = "showimg.php?thefile=" + thefile
theframe.processajax ("showimg",theloc);
}

function uploadimg (theform){
//Submit the form.
theform.submit();
//Then display a loading message to the user.
setStatus ("Loading...","showimg");
}

//Function to set a loading status.
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
if (obj){
obj.innerHTML = "<div class=\"bold\">" + theStatus + "</div>";
}
}

function changesize (img, sml){
//The display a loading message to the user.
theobj = document.getElementById("showimg");
if (theobj){
setStatus ("Loading...","showimg");
var loc = "thumb.php?img=" + img + "&sml=" + sml;
processajax ("showimg",loc);
}
}

Posted on 26 Oct 2009, 22:01 - Category: AJAX
No comments - Edit - Delete


AJAX xmlhttp.js get or post include

//Just save, upload, include
//xmlhttp.js

//Function to create an XMLHttp Object.
function getxmlhttp (){
//Create a boolean variable to check for a valid microsoft active X instance.
var xmlhttp = false;

//Check if we are using internet explorer.
try {
//If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
//Else we must be using a non-internet explorer browser.
xmlhttp = false;
}
}

//If we are using a non-internet explorer browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}

return xmlhttp;
}

//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
//Get an XMLHttpRequest object for use.
xmlhttp = getxmlhttp ();
if (getOrPost == "get"){
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
} else {
xmlhttp.open("POST", serverPage, true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(str);
}
}

Posted on 26 Oct 2009, 21:55 - Category: AJAX
No comments - Edit - Delete


Php simple draw calendar any month with table script

<?php

//calendar.php

//Check if the month and year values exist
if ((!$_GET['month']) && (!$_GET['year'])) {
$month = date ("n");
$year = date ("Y");
} else {
$month = $_GET['month'];
$year = $_GET['year'];
}

//Calculate the viewed month
$timestamp = mktime (0, 0, 0, $month, 1, $year);
$monthname = date("F", $timestamp);

//Now let's create the table with the proper month
?>
<table style="width: 105px; border-collapse: collapse;" border="1" cellpadding="3" cellspacing="0" bordercolor="#000000">
<tr>
<td colspan="7" class="calendartodayoff" onmouseover="this.className='calendartodayover'" onmouseout="this.className='calendartodayoff'">
<span style="font-weight: bold;"><?php echo $monthname . " " . $year; ?></span>
</td>
</tr>
<tr>
<td>
<span style="font-weight: bold;">Su</span>
</td>
<td>
<span style="font-weight: bold;">M</span>
</td>
<td>
<span style="font-weight: bold;">Tu</span>
</td>
<td>
<span style="font-weight: bold;">W</span>
</td>
<td>
<span style="font-weight: bold;">Th</span>
</td>
<td>
<span style="font-weight: bold;">F</span>
</td>
<td>
<span style="font-weight: bold;">Sa</span>
</td>
</tr>
<?php
$monthstart = date("w", $timestamp);
$lastday = date("d", mktime (0, 0, 0, $month + 1, 0, $year));
$startdate = -$monthstart;

//Figure out how many rows we need.
$numrows = ceil (((date("t",mktime (0, 0, 0, $month + 1, 0, $year)) + $monthstart) / 7));

//Let's make an appropriate number of rows...
for ($k = 1; $k <= $numrows; $k++){
?><tr><?php
//Use 7 columns (for 7 days)...
for ($i = 0; $i < 7; $i++){
$startdate++;
if (($startdate <= 0) || ($startdate > $lastday)){
//If we have a blank day in the calendar.
?><td style="background: #FFFFFF;">&nbsp;</td><?php
} else {

if ($startdate == date("j") && $month == date("n") && $year == date("Y")){
?><td><?php echo date ("j"); ?></td><?php
} else {
?><td><?php echo $startdate; ?></td><?php
}
}
}
?></tr><?php
}
?>
</table>

Posted on 26 Oct 2009, 21:41 - Category: Php
No comments - Edit - Delete


Pages: ... [4] [5] [6] [7] [8] [9] ...
Loading