Categories
PHP Tips & Tutorials

PHP: Session ID changes automatically at every request / page load

Ideally the initial session id will remain the same throughout a session unless the developer has explicitly asked PHP to change it by the session_regenerate_id() function or the session_id() function. However, there’s a slight chance that you may encounter this problem of automatic changing session ID upon every new request when you are developing things up, such as, on a localhost. You may be mad at PHP cause’ this way you won’t be able to use session id to track things out (which may be seldom, but still).

So what’s the problem?

You have set the browser to reject all cookie. You don’t know browsers track sessions via cookie?! HTTP is stateless, dumbass!

Categories
CSS & HTML Tips

CSS: Rotate Text / Image Elements by 90, 180 or 270 Degrees

As a quick digest of this article by Jonathan. You can literally rotate any HTML element by a certain degree with CSS, be it text or image or something else. After testing across a few modern browsers, the only one that’s not supporting this rotation technique is Opera.

The CSS rules you need for a rotation of 90 degrees:

/* for firefox, safari, chrome, etc. */
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
/* for ie */
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

That’s it.

Categories
JavaScript Tips & Tutorials

How to change the browser window status bar text of Firefox with JavaScript?

It’s strange that you can accomplish this in IE while you can’t make it happen in Firefox. Well, that’s because the development team has decided that it’s potentially annoying and harmful for web developers to change the text of Firefox status bar whatever way they want.

So the simple answer is, you can’t. But there’s always some sort of switch that would turn on or off the custom modification of the status bar text.

Simply type about:config in the Firefox address bar and it would take you to a vault of hidden advanced settings not meant for common users. Filter: disable_window_status_change and you will see that Firefox has come disabled of window status change by default.

firefox window status change

You can switch it off by changing the Value to false. However that ain’t gonna help you much because the vast majority of other Firefox users have left it in default, thus the JavaScript trick won’t work on their browsers.

Categories
Free PHP Classes & Library PHP Tips & Tutorials

PHP: Email Attachment Class

You can send emails very easily with PHP by the help of mail() function, but how does one send attachment attached to that email?

You’ll still use mail() function, but with a little more twists in the header argument – yes, the content of the attached file is actually encoded into the header of the email, can you believe that. Big head.

Unfortunately, it’s a pain in the ass every time you have to get through all the chores needed to attach and encode the file into the email header. You have to know the size of the file, base64_encode() it, chunk_split() it and set all the configurations required in the header so that the email client can recognize that something’s in the header and can extract it out as an attachment.

Anyway, I’ve made this class AttachmentEmail that you can email an attachment with PHP more easily. All the chores are taken care of. It’s written in a hurry so the structure can sure be better. Other than this, it works like a charm.

The Class:

class AttachmentEmail {
	private $from = '[email protected]';
	private $from_name = 'Your Name';
	private $reply_to = '[email protected]';
	private $to = '';
	private $subject = '';
	private $message = '';
	private $attachment = '';
	private $attachment_filename = '';

	public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
		$this -> to = $to;
		$this -> subject = $subject;
		$this -> message = $message;
		$this -> attachment = $attachment;
		$this -> attachment_filename = $attachment_filename;
	}

	public function mail() {
		if (!empty($this -> attachment)) {
			$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
			$path = dirname($this -> attachment);
			$mailto = $this -> to;
			$from_mail = $this -> from;
			$from_name = $this -> from_name;
			$replyto = $this -> reply_to;
			$subject = $this -> subject;
			$message = $this -> message;

			$file = $path.'/'.$filename;
			$file_size = filesize($file);
			$handle = fopen($file, "r");
			$content = fread($handle, $file_size);
			fclose($handle);
			$content = chunk_split(base64_encode($content));
			$uid = md5(uniqid(time()));
			$name = basename($file);
			$header = "From: ".$from_name." <".$from_mail.">\r\n";
			$header .= "Reply-To: ".$replyto."\r\n";
			$header .= "MIME-Version: 1.0\r\n";
			$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
			$header .= "This is a multi-part message in MIME format.\r\n";
			$header .= "--".$uid."\r\n";
			$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
			$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
			$header .= $message."\r\n\r\n";
			$header .= "--".$uid."\r\n";
			$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
			$header .= "Content-Transfer-Encoding: base64\r\n";
			$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
			$header .= $content."\r\n\r\n";
			$header .= "--".$uid."--";

			if (mail($mailto, $subject, "", $header)) {
				return true;
			} else {
				return false;
			}
		} else {
			$header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
			$header .= "Reply-To: ".($this -> reply_to)."\r\n";

			if (mail($this -> to, $this -> subject, $this -> message, $header)) {
				return true;
			} else {
				return false;
			}

		}
	}
}

The Usage:

For example, there’s this file residing on your web server at the path: /home/racker/gift.jpg and you want it attached to an email you are sending to Marry:

$sendit = new AttachmentEmail('[email protected]', 'Merry Christmas!', 'Hi', '/home/racker/gift.jpg');
$sendit -> mail();

The last argument ($attachment_filename) can be left empty if a filename is included in the second to last argument ($attachment). Otherwise you have to make clear what the file name is so the class can grab it and send it via attachment.

It returns true if the delivery is successful, otherwise false.

Categories
Kavoir & Whatever Web Design Tips and Insights

Key Differences between the Web Design of Google, Microsoft and Yahoo

Microsoft and Yahoo is colorful in colors but Google is colorful in the imagination.

Google uses much less colors than Microsoft and Yahoo do. Yet somehow, Google comes out as the versatile one.

Google uses much more traditional colors than Microsoft and Yahoo do and rarely change in the style, yet it comes out as the innovative one.

Google has much less web page elements than either Microsoft or Yahoo, yet it has come to be known as the sophisticated one.

Openness is rich in Google in that they design all of their pages with no distinct boundaries at all on a plain white blank sheet stretching as wide as the browser window / computer screen. On the contrary, Microsoft and Yahoo believes in borders, boxes and restrictions.

Categories
CSS & HTML Tips PHP Tips & Tutorials

How to display HTML code on a web page?

Web pages are made up of HTML code, so if you post or write some HTML code in a web pages, a browser would generally consider them normal code. Instead of showing them as they are literally, they have become part of the underlying HTML structure of that page.

Now how to address this problem and display HTML code and HTML tags correctly in a web page as the page content?

The answer is, use HTML entities.

HTML entities are a way to encode special HTML characters such as < and > into literal characters that cannot be interpreted by browsers as HTML code. Refer to the most common HTML entities conversion table below:

  • ‘&’ (ampersand) becomes ‘&amp;
  • ‘"’ (double quote) becomes ‘&quot;
  • ”’ (single quote) becomes ‘&#039;
  • ‘<‘ (less than) becomes ‘&lt;
  • ‘>’ (greater than) becomes ‘&gt;

Therefore, to display HTML code as they are and show them to your web site visitors, write the special characters such as < and > in HTML entities. For example,

<h2>Beautiful town</h2>

Has to be written:

&lt;h2&gt;Beautiful town&lt;/h2&gt;

There are of course handy tools you can use to make these conversions much easier. PHP has a function, htmlentities() that accepts normal HTML code and converts them into HTML entities. You can also rely on online tools to make that happen.

Categories
CSS & HTML Tips

How to Create Blinking Text in HTML

Blinking texts are so 1999. Please by all means if you are still looking for the way to make text blink in HTML web pages, stop.

Otherwise, you may want to try these 2 outdated methods that only work partially across modern browsers, but not in IE:

The <blink> tag

<blink>I'm blinking!/<blink>

The blink text-decoration CSS rule property

The HTML:

<h1>Blinking texts are out</h1>

The CSS:

h1 {text-decoration:blink;}

Categories
Information Security PHP Tips & Tutorials

One Simple Way to Encrypt, Obfuscate, Hide or Protect Your PHP Code

This way is so simple that anyone who’s a beginner in PHP can use it immediately to obfuscate and hide the original PHP code. Generally, it’d make it much harder for someone to find a specific phrase in your code as it’s encrypted, though in a rather simple way using 4 PHP functions: gzinflate(), gzdeflate(), base64_encode() and base64_decode().

For example, you can make it eventually impossible for someone who know nothing about PHP or programming to modify your code and some of your native strings. It comes quite handy in encoding, obfuscating and protecting your credits lines in the footer of your scripts of web software.

Say here is the line of code you want to hide from being modified:

echo "You can't find me!"; // it’s “echo” instead of “echo”, same below. Have to post them this way because of a WP bug.

You can get the obfuscated and encrypted version of this line of code by:

echo base64_encode(gzdeflate('echo "You can\'t find me!";'));

Which would output:

S03OyFdQiswvVUhOzFMvUUjLzEtRyE1VVLIGAA==

This is the code you should use in your script. As it’s all encrypted and obfuscated, the original string and code are totally hidden, protecting them from being changed.

To run the hidden code, replace the original line of code with this one:

eval(gzinflate(base64_decode('S03OyFdQiswvVUhOzFMvUUjLzEtRyE1VVLIGAA==')));

It’s simply the reverse of the encoding plus an eval() function of PHP. And because all the original code and strings are totally encrypted in obfuscation, it’d be harder for non-programmers to modify your script but not professionals. For absolute protection of your code, use Zend Guard.

Categories
SQL / MySQL Tips and Tutorials

What are New Line Feed and Carriage Return in a MySQL query?

We all know what a new line is represented in c and in PHP as:

"\n"

And a carriage return:

"\r"

In a SQL / MySQL query, when you want to add new lines or line feeds into the strings, you will need the CHAR() function of MySQL which accepts an ASCII character code as input and returns a character accordingly. For example, to search a specific column for all new lines and erase them:

UPDATE sometable SET somecolumn = REPLACE(somecolumn, CHAR(10), ''); // CHAR(10) is a new line

This could get the thing done in most cases. However in some systems new lines are a combination of a new line and a carriage return, namely "\r\n". In this case, you will need CHAR(13):

UPDATE sometable SET somecolumn = REPLACE(somecolumn, CHAR(13) + CHAR(10), ''); // CHAR(13) is a carriage return and the plus sign is how strings are concatenated in MySQL.

Categories
Hosting Tips & Deals Linux Server Administration Tips

Linux wget Command to Download and Mirror a Website in Static Local Copy

wget is such a wonderful command in Linux you can ever get. Consider blessed to have it in your SSH arsenal. Now, not only does it allow you to download something neatly from the command line to the current working directory – that’s why WordPress always puts the latest version of the blog script at http://www.wordpress.org/latest.zip, that way you don’t even have to visit the site before using wget to get it in no hassle:

wget http://www.wordpress.org/latest.zip

And the latest version of WordPress will be in your local working directory in no time – but it can also help you to create a local mirrored copy of a remote website for backup or for local browsing.

How to Make Mirrors from a Site with wget command?

wget comes equipped with so many useful switches and features and one of them is to mirror stuff off the Internet, from online to offline. For example, you have a blogspot blog and you want a local copy of the entire blog files / web pages, including all those CSS style sheets, images and scripts. No problem, just go with

wget -mk http://yours.blogspot.com

Don’t worry, wget will create a new directory named by the domain, in this case, "yours.blogspot.com", to store all those files. After screens and screens of real-time logging, when it’s done, the local mirror should be ready to use.

The –m switch is short for –mirror and the –k switch translates all absolute URLs in the HTML into relative ones for totally offline browsing.

The problem is that wget works much better with sites using sanitized / clean URLs such as a WordPress blog. Though a blogspot blog is basically in static HTML, it contains quite some complicated URLs here and there that’s filled with parameters. The result doesn’t look too pretty.

However, after trying wget to recursively download and mirror a WordPress blog, it looks fantastic. Try it with a permalink enabled WordPress blog and it will surely amaze you.

It’s best used to generate mirrors of websites completely built in static files or sites with simple URL schemes.