Tuesday, August 6, 2013

Apostrophe (') Gets Censored After Inserting Into MySQL. How to Fix it?

I think MySQL doesn't like apostrophe or '. When inserting into MySQL, the apostrophe goes missing. How to fix this? Simple. Here's it is:

$text = "My car's mirror is broken!";
$text2DB = addslashes($text);
mysql_query("INSERT INTO ... $text2DB ... // complete the MySQL INSERT INTO action here



PHP's addslashes() function is enough to solve this problem. It will add slashes when necessary automatically. Isn't that neat?

Read More »

From Your Check Out Form to Paypal's Note to Seller


If you want to add note to the Paypal seller from your check out form or shopping cart, you need to add the cn input name and value in the post form before submitting to Paypal. Something look like:

<input type=hidden name=cn value="Please ship directly to my dorm room">


If everything is done correctly, the "note to seller" will appear in the Paypal check out form.
Read More »

Auto Login using PHP stream_context_create() and file_get_contents()


The following is just a rough idea of how to log in to a website using PHP. If the target website is owned by you, you should know how it is done. If not, you may need to contact the web administrator before doing so. The web site owner may not like it. The main idea here is to make the login and grab the data out with just one push button to save all the hassels to log in manually. It is advised to make it completely legal before you even try it:

$postdata = http_build_query(
 array(
     'postUserName' => 'yourLoginID',
     'postPassword' => 'yourPassword'
 )
);

$postOptions = array('http' =>
 array(
     'method' => 'POST',
     'header' => 'Cookie: PHPSESSID='.$sessid,
     'content' => $postdata,
     'user_agent'=> $_SERVER['HTTP_USER_AGENT']
 )
);

$context = stream_context_create($postOptions);

$return = file_get_contents("http://yourtargetloginURL.com/targetlogin.php?condition=login", false, $context);
. // check $return if it is compressed. If so, uncompressed it before anything
. // check $return HTML for success/fail login
. // set cookie for successful login
...



For those in green, it depends on your own login environment. Every login situation is different. Some requires certain cookie to be set prior to anything else. Finally, make sure it is legal before you go ahead.

Read More »

My Javascript and PHP Way to Extract the File Extension

Javascript:

var extension;
var filename = "test.my.txt";
var ext = filename.split(".");
if (ext[1] != "") {
     extension = ext[ext.length-1];
}

// extension will contain "txt"


PHP:

$fullfilename = "test.my.txt";
preg_match("/(.+)\.([^\.]+)$/",$fullfilename,$match);
$filename = $match[1];
$extension = $match[2]; // $extension will contain "txt"



I think the codes are very self-explanatory. Enjoy!!!

Read More »

Javascript and PHP Cookie Expiration Setting Technique

Javascript:

var hour = 24; // expire in 24 hours
var tm1 = new Date();
var tm2 = new Date(tm1.getTime()+3600000*hour); // 60*60*1000 = 3600000 ms
var exp = "expires = " + tm2.toUTCString();

document.cookie = 'cookiename = cookievalue;'+exp;


PHP:

$hour = 24;
setcookie("cookiename","cookievalue",time()+(3600*$hour));



For PHP technique, it has a problem. The time() gets the time of the server instead of the browser. In order to get the accurate expiration timing, one needs to either rely on Javascript to set the cookie or obtain the time zone from the browser as I mentioned in my earlier post.

Read More »

My Javascript to Guess Processor's Speed



Updated on 5th of February, 2014: There is an updated version for this posting here.



This is necessary when your web page runs a lots of animation using javascript. If the target browser is slow in computing speed, it is advisable to reduce the amount of animation to run on the machine.

Here's my simple way to check the speed of a computer using simple Javascript:

<script>

var count;
var d = new Date();
var startTime = d.getTime();

for (i=0; i<=1000000; i++) {
count++;
}

var d = new Date();
var endTime = d.getTime();
var myLatency = endTime - startTime;

</script>



myLatency contains my speed index.

After testing using this script, iPad 1 will give myLatency more than 200, iPad 2 between 100 and 200, and iPad 3/4 less than 100.

As for the PC/Mac, myLatency is around 20-100 for dual core processor and quad core processor less than 10. Anyway, the myLatency also depends on the tasks the computer is running in the background concurrently and the core speed in GHz. Anyway, it is just a guess speed Javascript and it should not be treated as a sure thing for the speed test.

Read More »

Monday, August 5, 2013

My Javascript Function to Show UTC Time Zone of Your Browser/Computer


Your computer has its own time zone setting as well as the web host server. Most of the time they are quite different. United States alone has three different time zones.

It is crucial to get the time zone offset off the browser than the server as the time zone is something personal rather than a server sitting on a fixed location that tells nothing about the visitors possibly from the other side of the globe.

Here is a simple Javascript function to get the UTC time zone offset of your browser:

function getBrowserUTCTimezone() {
   x = new Date();
   return x.getTimezoneOffset()/-60;
}



Please note that the offset returned by the Javascript function getTimezoneOffset() will be divided by -60. Why negative? I'm not so sure. The invert of the number seems to give the right offset every time.
Read More »

Sunday, August 4, 2013

JQuery Close-Up: Is it a Good Idea?


JQuery popularity in web development nowadays is similar to Facebook in social network. Developers and programmers love to use it.

The coding is simple and brisk but most people use it just to perform simple tasks on the web pages such as changing the DOM element styles, ie. color, opacity and location.

Many use it to perform tasks that can be easily emulated in conventional Javascript and I think it is over-killing. I now dig a bit digger into JQuery and see if it is worth to have it on your web pages.


Please take a look of the following network performance of a JQuery in a web page. I'm using a slow connection. Pardon me. :)




Do note that the size of jquery-1.9.1.min.js is around 90kB. For slow connections, it is a burden for a web page to loaded with it. But for a fast internet network, the size is acceptable. Please bear in mind that for public internet facilities, the network could be crawling if many of the users are watching video streamed at the same time.

And here's the good news. Browsers nowadays will be able to cache the JQuery library and reuse it when any web pages call for it. Since many web pages (I can almost assume almost all web pages) are using it, the browser most likely won't even need to reload the library when your web pages call for it. Here's the proof of what happened when I reloaded the same web page the 2nd time:




The news gets even better if you load from Google API server. As you can see the library is somewhat "compressed" thus reducing the size to merely 32kB.





My conclusion is that it depends on your reliance on the handy library. If your task to be performed can be easily replaced by simple Javascript, avoid including the library. Who knows somebody might be running your web page the first time without a cache in the browser after having the cache cleared or on a newly installed browser. If the tasks to be performed on a web page are complicated and hard to be replaced by conventional Javascript or the scripts will be too long, go for it!

Read More »

Modifying Your ASP Code to ASPX VB.Net

This could have been a very old topic as ASP (VB) has been phased out long ago and VB.Net is almost obsolete compared with C#. Although VB and VB.net are using the same programming language, the way they work on a web page is vastly different.

Anyway, let's go with the fun of migrating from one programming environment to another.

ASP (VB)

<%
.
.
Dim orange
orange = "apple"
.
.
.
%>

<html>
<body>
.
.
...<%=orange%> . . .
.
.
.
</body>

</html>



ASPX (VB.Net)

<%@ import Namespace="System.IO" %>
<%@ Page Language="vb" Debug="true" %>

<script language="vb" runat="server">

Private orange as String

Sub Page_Load()
.
.
orange = "apple"
.
.
End Sub

</script>

<html>

<body>
.
.
...<% Response.Write(orange) %> . . .
.
.
.
</body>

</html>



It's hard to get used to the new structure of VB.net initially. But once you get it done error-free a few times, you'll get used to it. There are also some syntax changes in VB.net compared to the old VB such as the use of "+" to concatenate strings together instead of using "&".

Anyway, that's all for now and happy coding!!!

Read More »