PHP nuggets
Solutions and tips I've found useful in PHP code development.
Thursday, September 4, 2014
Detecting browser type
function getBrowserType()
{
$agent = "";
$browser = "";
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if(strlen(strstr($agent,'Safari')) > 0) {$browser = 'Safari';}
if (false) {}
elseif (strlen(strstr($agent,'Chrome')) > 0 ) {$browser = 'Chrome';}
elseif (strlen(strstr($agent,'Firefox')) > 0 ) {$browser = 'Firefox';}
elseif(strlen(strstr($agent,'MSIE')) > 0 ) {$browser = 'Internet Explorer';}
elseif(strlen(strstr($agent,'Safari')) > 0 ) {$browser = 'Safari';}
else {$browser="?";}
return $browser;
}
Tuesday, December 21, 2010
PHP mail() won't work with IIS?
Couldn't get PHP mail() to work. My problem was this:
phpinfo() showed that the SMTP parameter was set to 'localhost' in php.ini, which I thought should have worked.
Changing this to the explicit IP of the SMTP got mail() working! I don't have direct access to php.ini, so at the top of my email function I added:
ini_set('SMTP', '10.xx.xx.xx'); // -where 10.xx.xx.xx represents the correct explicit value.
phpinfo() showed that the SMTP parameter was set to 'localhost' in php.ini, which I thought should have worked.
Changing this to the explicit IP of the SMTP got mail() working! I don't have direct access to php.ini, so at the top of my email function I added:
ini_set('SMTP', '10.xx.xx.xx'); // -where 10.xx.xx.xx represents the correct explicit value.
Problems uploading files?
About permissions of upload_tmp_dir: http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/
Monday, August 23, 2010
How to open multiple links in new tabs, using javascript
I wanted to click a button, and have several new browser tabs open to specific websites.
This seems to work in Firefox 3.0.19, but not in IE8.
Any suggestions on a solution for both browsers?
This seems to work in Firefox 3.0.19, but not in IE8.
Any suggestions on a solution for both browsers?
<script language="javascript"> window.open('http://www...', '1 _newtab'); window.open('http://www...', '2 _newtab'); window.open('http://www...', '3 _newtab'); </script>
Monday, August 16, 2010
How to remove unused GET parameters from a page's URL
I've several pages that accept numerous GET parameters from the URL, but sometimes few if any of these parameters are provided. I want to strip the blank parameters from the URL and direct the browser to that link, so that the URL is more readable, and so that users may copy and paste or bookmark a more concise URL.
Near the top of my PHP script I call the shrink_url() function, which I have preloaded with a require_once("funcs.php") statement. funcs.php includes this function:
function shrink_url()
{
$currenturl =curPageURL();
$servername = $_SERVER["SERVER_NAME"];
$scriptname = $_SERVER["SCRIPT_NAME"];
foreach ($_GET as $key => $value)
{
if ($value <> "") {$getparams .= "&" . $key . "=" . urlencode($value);}
}
$getparams = substr($getparams,1);
if ($getparams <> "") $getparams = "?" . $getparams;
$redirect = "http://" . $servername . $scriptname . $getparams;
if ($currenturl <> $redirect)
{
ob_end_clean(); //stops loading the current page.
header("Location: $redirect"); //redirects to the shrunken URL.
}
}
Near the top of my PHP script I call the shrink_url() function, which I have preloaded with a require_once("funcs.php") statement. funcs.php includes this function:
{
$currenturl =curPageURL();
$servername = $_SERVER["SERVER_NAME"];
$scriptname = $_SERVER["SCRIPT_NAME"];
foreach ($_GET as $key => $value)
{
if ($value <> "") {$getparams .= "&" . $key . "=" . urlencode($value);}
}
$getparams = substr($getparams,1);
if ($getparams <> "") $getparams = "?" . $getparams;
$redirect = "http://" . $servername . $scriptname . $getparams;
if ($currenturl <> $redirect)
{
ob_end_clean(); //stops loading the current page.
header("Location: $redirect"); //redirects to the shrunken URL.
}
}
Thursday, August 12, 2010
PHP CURL
Sending POST form data with php CURL
This site also has links to resources on Ajax, CSS, Javascript, XHTML, and more.
This brief guide explains 2 different reasons for wanting to send POST data with curl and how to do it. It is very handy to have the abililty to arbitrarily send POST data from a form to a script.
This site also has links to resources on Ajax, CSS, Javascript, XHTML, and more.
Wednesday, July 28, 2010
Looking for a good source code editor? Consider Notepad++
Notepad++ offers some nice features, including FTP. Plus, it's free.
See: http://notepad-plus-plus.org/
See: http://notepad-plus-plus.org/
Thursday, July 1, 2010
HEREDOC: a great way to build long strings
The PHP HEREDOC feature: a great way to build long strings, such as when You're writing PHP code to spit out HTML. For example:
Hideous old method:
Magnificent Heredoc method:
see: http://www.tizag.com/phpT/strings.php
Note that the Heredoc syntax is rather unforgiving. That terminating "COOLSTUFF" line must be NON-indented, and You can't even have spaces or tabs after it on the same line, or it won't work. But if You treat it nice, it will treat You nice.
Hideous old method:
$my_string = "".
"<table>\n" .
" <tr>\n".
" <td>What's for lunch?</td>\n".
" </tr>\n".
"</table>\n";
echo $my_string;
Magnificent Heredoc method:
$my_string = <<<COOLSTUFF
<table>
<tr>
<td>What's for lunch?</td>
</tr>
</table>
COOLSTUFF;
echo $my_string;
see: http://www.tizag.com/phpT/strings.php
Note that the Heredoc syntax is rather unforgiving. That terminating "COOLSTUFF" line must be NON-indented, and You can't even have spaces or tabs after it on the same line, or it won't work. But if You treat it nice, it will treat You nice.
Variables in a CSS style sheet
Here's something I learned about PHP yesterday that was just so much fun I have to share it with You. Of course, You probably learned this on Your mother's knee:
I found out how to define and use variables in a CSS style sheet. Very convenient when You want to, say, define a color once in Your CSS sheet and then use it multiple times throughout that CSS file. I gather that normally CSS doesn't support variables, but there's a nice way around that limitation. It basically involves using a style.php file rather than a style.css file, and then embedding variables in the CSS with php. see: http://sperling.com/examples/ pcss/
I found out how to define and use variables in a CSS style sheet. Very convenient when You want to, say, define a color once in Your CSS sheet and then use it multiple times throughout that CSS file. I gather that normally CSS doesn't support variables, but there's a nice way around that limitation. It basically involves using a style.php file rather than a style.css file, and then embedding variables in the CSS with php. see: http://sperling.com/examples/
Subscribe to:
Posts (Atom)