User Tools

Site Tools


php

This is an old revision of the document!


PHP

Here are some snippets for if you're stuck writing PHP.

# full current URL
function current_url() {
	$pageURL = 'http';
	if (@$_SERVER["HTTPS"] == "on") $pageURL .= "s";
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return $pageURL;
}
 
# URL to this script, skipping parameters, and not excising "index.php"
function script_url() {
	$pageURL = 'http';
	if (@$_SERVER["HTTPS"] == "on") $pageURL .= "s";
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["SCRIPT_NAME"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
	}
	return $pageURL;
}
 
# true if string $str ends with $test
function endswith($str,$test) {
	return substr_compare($str, $test, -strlen($test), strlen($test)) === 0;
}

Android App Inventor TinyWebDB interface

Android is a mobile platform. App Inventor lets you make apps quickly and easily for Android without coding. TinyWebDB is a web transaction mechanism originally intended for storing data remotely, but which can be co-opted for any web-based operations. Here's some code that will speak TinyWebDB protocol for both the get & set methods. If you go to this script with a regular browser, it will tell you what the service URL should be.

<?php
 
# URL to this script, skipping parameters, and not excising "index.php"
function script_url() {
	$pageURL = 'http';
	if (@$_SERVER["HTTPS"] == "on") $pageURL .= "s";
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["SCRIPT_NAME"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
	}
	return $pageURL;
}
 
$uri = $_SERVER["REQUEST_URI"]; 
if (strpos($uri,'storeavalue')) {
	// We are storing a value in this post
	$tag =trim($_POST["tag"]);
	$value =trim($_POST["value"]); 
 
	// TODO: Do something with those values
 
	echo '["STORED", "$tag", "$value"]'; 
 
} elseif (strpos($uri,'getvalue')) {
	// We are retrieving a value in this post
	$tag = trim($_POST["tag"]);
 
	// TODO; figure out value
	$value = "RETURN_VALUE!!";
	$value = array(1,2,3,"dogs",4);
 
	echo json_encode(array("VALUE", $tag, $value));
} else {
	$url = script_url() . '/';
	echo "<html><body><h1>TinyWebDB interface</h1>Hello.  Point your TinyWebDB-based app to this location: <a href='$url'>$url</a></body></html>";
}
?>
php.1286585678.txt.gz · Last modified: 2010/10/08 17:54 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki