Recent Post

Categories

Archives

Cow Computing

10 Jul 23

cURL Post Follow Location And Update Browser Address Bar

Often time, one might be using cURL to perform some post/get operations. Out of all, there’s a scenario where one might want to do a form post and have the user redirected thru the 302 status code issued by the server. This can be easily implemented using cURL, however, there’s a problem — The address bar doesn’t update to the redirected URL. (e.g. Posting a form from “form.php”, and redirected to “result.php”, but then the browser’s address bar remains showing “form.php”).

In order to solve the above problem, here’s a little trick:

<?php

// post param
$param = array('data' => 'abc');

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "form.php");
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $param);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_exec($c);

// here's the trick
$redirectLocation = curl_getinfo($c, CURLINFO_EFFECTIVE_URL);
header("Location: $redirectLocation");

?>

10 Apr 4

PHP Data Object

Often time, when working on a website, certain database transactions have to be taken with great care (e.g. payment, account creation…). It’s definitely an advantage to make use of the Atomicity the database provides (e.g. mySQL). But How? Here’s i will show you how to use PDO to wrap a SQL insert statement such that it’s either done or no. (*it’s included with PHP5, and you have to enable the extension inside the php.ini)

<?php

$hostname = 'localhost';
$username = 'username';
$password = 'password';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);

// Setting the notification to throw exception when error encountered
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$dbh->beginTransaction();
$count = $dbh->exec("INSERT INTO user(userID, name) VALUES ('1', 'Steve')");
$dbh->commit();

$dbh = null;
}
catch(PDOException $e)
{
$dbh->rollback();
echo $e->getMessage();
}

?>

10 Mar 8

WordPress Error: Cannot Modify Header Information

Have you ever encountered the error: “Cannot modify header information” when you try to access the admin page of wordpress? The debug message might have suggested you to look at “pluggable.php”. When searching thru google, people often advise you to turn off installed-plugin one by one to see if the problem get resolved.

The above might work, but i would suggest you to try the following method before going thru the turn-off plugin cycle. For example, if you have modified “functions.php”, and the above header information header appeared right after, you can easily resolve this by removing all the extra whitespace before the “<?php” and “?>” tag. Viewing thru editor, you might not notice any whitespace, but try this trick:

  1. Remove the “>” from the “?>” tag at the end of file
  2. Then keep holding Delete key (not backspace) for a few seconds. (*note that the cursor should be locate after the “?”)
  3. You will notice some whitespace being deleted. Amazing isn’t it?
  4. Add the “>” after the “?” to properly close the php tag
  5. Save the file, and try accessing it, the header information error should have been resolved

10 Feb 7

HTML 5 nth-Child

As we all know, HTML 5 is on its way, and there’s going to be a huge addition to the current syntax. In this post, i shall share with you about the “nth-child” feature. Often, we would like to create a table or list with alternate row color as follow:

// HTML Snippet
<div id="test-area">
  <ul>
    <li>row 1</li>
    <li class="even-row">row 2</li>
    <li>row 3</li>
    <li class="even-row">row 4</li>
    <li>row 5</li>
    <li class="even-row">row 6</li>
    <li>row 7</li>
    <li class="even-row">row 8</li>
    <li>row 9</li>
    <li class="even-row">row 10</li>
  </ul>
</div>
// CSS Snippet
ul {
  list-style: none;
}

// color the even rows
.even-row {
  background-color: #ccc;
}

Read More / Comment »

09 Dec 8

Chinese URL — Good or Bad?

ICANN has announced the open-up of Non-Latin Characters’ URL for registration. If everything is good, the service will commenced mid-2010. So there will be another battle of url registration.

Afterall, is having Chinese URL available, good or bad?

On the POSITIVE side: First, it creates lots more space/possibility for meaningful URL, as current latin url has been exhaustively registered by bulk registration. Meaningful and Short URL is hard to create. Second, chinese url is more familiar to chinese people and they can easily remembered the address. Third, It’s another $$ opportunity. Lastly, some proper rule could be implemented on top of the new registration procedure to reduce the chance of abusing url registration.

On the NEGATIVE side: First, chinese url doesn’t sound meaningful to any foreigner, and in particular, it requires users to know chinese typing in order to access the website, which limits business (of course, it could be solved by purchasing TWO sets of URL, Chinese and English). Second, on the technical aspect, it creates trouble for writing regular expression, since some programming language like PHP, doesn’t have a easy way to match unicode characters, and another set of regular expression specifically addressing unicode would need to be added to current working web app to extend its functionality to accommodate chinese url (e.g. to validate website address / email address). Third, headers (e.g. HTTP, SOAP or so…) will need to be able to handle unicode or at least Chinese Character, which is another big changes.

It could be seen that Chinese URL has its own use, but is it necessary?