Tag Archives: php

How to display all PHP server variables

Sometimes I get confused with what result a server variables would return.  For example between the $_SERVER[‘SCRIPTNAME’] and $_SERVER[‘SCRIPT_FILENAME’].

Use the following code to display all server variables. From there you can decide which server variable is suitable to use.

1
2
3
4
echo "<table>";
foreach($_SERVER as $key=>$value)
        echo "<tr><td>$key</td><td>$value</td></tr>";
echo "</table>";

PHP function for pagination/paging

Found this function. Not tested yet but maybe useful in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// example variable
$sql_limit = (isset($_GET['limit'])) ? $_GET['limit'] : 0;
 
function navigation_links($curr_limit, $num_records, $limit_val, $limit_var = "limit", $next = "next >", $prev = "< prev", $seperator = "|") {
    // rebuild query string
    if (!empty($_SERVER['QUERY_STRING'])) {
        $parts = explode("&", $_SERVER['QUERY_STRING']);
        $newParts = array();
        foreach ($parts as $val) {
            if (stristr($val, $limit_var) == false) array_push($newParts, $val);
        }
        $qs = (count($newParts) > 0) ? "&".implode("&", $newParts) : "";
    } else {
        $qs = "";
    }
    $navi = "";
    if ($curr_limit > 0) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit-$limit_val).$qs."">".$prev."</a>";
    }
    $navi .= " ".$seperator." ";
    if ($curr_limit < ($num_records-$limit_val)) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit+$limit_val).$qs."">".$next."</a>";
    }
    return trim($navi, " | ");
}
 
// example placing links ($num_all is the value of all records in you result set)
echo navigation_links($sql_limit, $num_all, 10);
?>

source | more functions avail.

PHP header forwarding not working

If you are finding that header() is not working for no obvious reason, make use of the headers_sent() function to drill down to the cause of the problem.

Consider the following scenario,

1
2
3
4
5
6
7
<?php
// Sign out
signOut();
 
// Redirect back to the main page
header("Location: http://mysite.com/mainpage");
?>

If for some reason, the header() call to redirect is not working, there won’t be any error messages, making it difficult to debug.  However, using headers_sent(), you may easily find the source of the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
// Sign out
signOut();
 
/*
* If headers were already sent for some reason,
* the upcoming call to header() will not work...
*/
if(headers_sent($file, $line)){
// ... where were the mysterious headers sent from?
echo "Headers were already sent in $file on line $line...";
}
 
// Redirect back to the main page
header("Location: http://mysite.com/mainpage");
?>

source (comment by jasper)

PHP code to check email validity

This PHP code can be used to check whether email is valid or not by ILoveJackDaniel. It is a bit lengthy compared to other script, but this one is more comprehensive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function check_email_address($email) {
  // First, we check that there's one @ symbol,
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not,
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}

Rapid application development – which framework to use?

I always want to do a very fast application development. This can only be done with what we call a “framework”. I have been doing a lot of research on popular PHP framework but not yet decided which one to use.

Some of them are cakephp, codeigniter, symphony and rapyd.

After using it for some time will plan to come out with own framework for better control and easy modification on the code.

Inserting source code in a blog post

I’ve been looking for WP plugin to add source code in blog posting. I want to share my code sample via the blog. At last I found this WP-Syntax plugin.

Here how a source code will look like

1
2
3
<?php
  echo "Hello World!";
?>

You need to add <pre lang=”php” line=”1″> tag before and </pre> tag after the source code. The parameter line=1 is to show the line number

PHP script to get domain from URL string

Sometimes maybe you need to get a domain name from a URL string. Here is the code

1
2
3
4
5
6
7
8
9
10
11
12
function GetDomain($url)
{
$nowww = ereg_replace(’www.,,$url);
$domain = parse_url($nowww);
if(!empty($domain["host"]))
    {
     return $domain["host"];
     } else
     {
     return $domain["path"];
     }
}

source