User Tools

Site Tools


php

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   = $_POST["tag"];
	$value = json_decode($_POST["value"]);
 
	# $value may be a string or array depending on what you sent in your app
	
	// TODO: Do something with those values
 
	echo json_encode(array("STORED", $tag, $value));
 
} elseif (strpos($uri,'getvalue')) {
	// We are retrieving a value in this post
	$tag = $_POST["tag"];
 
	// TODO; figure out value
	$value = "RETURN_VALUE!!"; 	# $value may be a string or array depending on what your app is set up to receive
	
	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.txt · Last modified: 2010/10/08 18:58 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki