Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, 10 August 2012

The Right way to Redirect a Page

Well it seems like every time I do something new with my website, there is always something to learn. I'm in the process of rewriting the CMS I use for the how to's and examples on http://blueskyworkshop.com/topics/. I know I needed to use an HTTP header to forward pages. But after some additional reading, I learned what I was doing was not enough.



To tell a search engine like Google a page has moved, you have to send it a 301 Moved Permanently header. Then, the next time Google's spider reads the site, it will move the statistics associated with the page from the old location to the new one. This is done in PHP as follows:

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.somacon.com/");
exit();
?>
Get the details from Somacon here. The post includes a number of examples for several different languages.



I should be able to confirm the older pages are no longer being referenced using Google Analytics. Anyway, I guess we will see in a few weeks.

Friday, 25 May 2012

Coda and Diet Coda on Sale Today Only (5/25)

Are you a fan of HTML, CSS, Javascript, and PHP development on the Mac? If so then you are probably a fan of Coda, which is a Web Site development tool for OS X made by Panic Software.



New Version 2

Well the new version 2, is out this week. Panic is offering Coda at 50% off this week only for early purchasers Today, Friday 5/25, is the last day of the deal so if you are interested, head to the Mac app store and get code for $49.



Coda 2





Diet Coda

In addition to Coda 2, Panic is releasing a programming editor for the iPad called Diet Coda. I have always found the idea of programming on iPad type devices intriguing. So for $9.99, definitely give this app a look. It looks like it might make the whole coding thing work on a pad.



Diet Coda for the iPad.




Sunday, 20 March 2011

How To Pages Updated

Over this weekend, I updated all of my how to pages on http://blueskyworkshop.com. The main change is new and improved PHP templates. The changes were necessary to update all my Google search boxes to a new format. I also discovered I had left out the Google Analytics telemetry out of my Java Pages. All that is fixed now.



The topics are linked on the right side of the page and also here.



Friday, 24 December 2010

Detecting Browsers with PHP

PHP LogoBeen looking for a way to detect a browser in PHP. Blogger has added a feature to automatically deliver a mobile web version of my blog. However, this doesn't seem to work well with my frameset setup on blueskyworkshop.com. Plus blogger doesn't include any of my navigation features or ads on the mobile version. So I want to roll my own.



So here are a couple links on browser detection.

Monday, 6 September 2010

PHP: Call to a member function on a non-object

PHP LogoRan across a PHP thinking in Java problem today. Say you have some code like this:



class MyClass{
private $myObject;

public function MyClass(){
$myObject = new MyObject()
}

}
As soon as the $myObject variable is accessed the following error is thrown: Call to a member function on a non-object.



What?????



This is PHP not Java nimrod. :) $myObject is not getting initialized because the syntax is incorrect. Try this:

class MyClass{
private $myObject;

public function MyClass(){
$this->myObject = new MyObject()
}

}
That should fix it right up. This sort of stuff always happens to me as I bounce between languages. lol.

Wednesday, 28 April 2010

Paging Data with CodeIgniter

I was looking into how one would page table data using CodeIgniter. So I didn't find one answer, but found a PHP answer and the CodeIgniter documentation to answer my own question.



The PHP technique is to add a LIMIT statement to your SQL request. So if you have a table size and a location, you can page through the data easily. The php-mysql-tutorial.com has the details here. Marry this to the CodeIgniter active record documentation, and that should be all I need.

Wednesday, 7 April 2010

CodeIgniter Unit Testing

CodeIgniter includes a unit test library as part of the framework. Documentation is included with the standard docs. If you follow the instructions, you can set it up and use it quite easily. However, the default output format leaves a little bit to be desired. Each test generates a separate table. This quickly becomes unwieldy and not very useful as the more tests added create longer and longer pages.



To solve the problem modified the template a little so once a table is produced with a row for each test. Here is my code.



   1:  <h2>Unit Test Page</h2>
2: <p>This is the test page for the application.</p>
3:<?php
4:$str = '
5: <tr>
6: {rows}
7: <td style="border:1pt solid black;">{result}</td>
8: {/rows}
9: </tr>';
10:
11:$this->unit->set_template($str);
12:
13:// Sample Test 1
14:$test = 1 + 1;
15:$expected_result = 2;
16:$test_name = 'Adds one plus one';
17:$this->unit->run($test, $expected_result, $test_name);
18:
19:
20:?>
21:<table cellpadding="4" cellspacing="1" style="border:1pt solid black; font-size:9pt; width:625px">
22: <tr><th width="35%">Test Name</th><th>In Type</th><th>Test Type</th><th width="15%">Result</th><th width="0%">Path</th><th>No</th></tr>
23:<?php
24:// Create Report
25:echo $this->unit->report();
26:?>
27:</table>
28:
29:</body>
30:</html>


My template is defined on lines 4-9. Basically, we only want to output a row for each test. So lines 20-27 defines the table that contains the rows. This produces much more compressed output.

Thursday, 26 November 2009

Apache, MySQL and PHP for Windows 7 Starter



Windows 7 starter edition is pretty bare bones. So when I decided I wanted to look into doing some limited web development on my netbook, I needed to install something very small.  After Googling around a bit I found this server: http://www.uniformserver.com/



The Uniform Server is a Windows, Apache, MySQL, PHP bundle for Windows designed to work off flash drives. The whole package zips into a file that is only 10 megs. In addition, all the software runs from the directory the software is unzipped into. No registry changes needed. Just run the start.exe program and everything runs. Very cool.

Saturday, 17 October 2009

Monday, 5 October 2009

MySQL Getting Started Guide

MySQL Logo

I actually posted a short MySQL guide a week or two ago, but haven't blogged about it.The Getting Started with MySQL page is a little cheat sheet to help me set up and administer a MySQL server and DB.



It certainly has helped with my CodeIgniter projects. It is not finished yet, but hopefully there is enough there to help others.

Monday, 20 July 2009

Added a Simple Cookie Example


PHP Logo

I have added a very simple cookie example to my how to pages. It shows how to set a cookie and read it back with PHP. Just wanted some simple examples for my own reference. For detailed information on the subject I recommend the following book.


PHP Cookbook (Cookbooks (O'Reilly))

Saturday, 20 June 2009

Get the File Name of the Current PHP Script


PHP Logo

Needed to get the name of the current script in PHP. I found this snippet on exactly how to do that. So I must give credit where credit is due. :)

Wednesday, 17 June 2009

Workaround for Textile 2.0 PHP 5.2.x Bug


PHP Logo

At work we use the Confluence wiki quite a bit. The wiki markup is based on the textile markup language. Well to save myself some time, I decided I would download the textile php class and work on my document locally. So I downloaded the 2.0.0 zip of the PHP file, installed it on my web server, and linked the document I'm working on. My PHP script worked, but the list output was all messed up. Every list generated by textile would look like this:



<ul>
<li>Item One</li>
<li>Item Two</li>
</ul>
<ul>
<li>Item Three</li>
</ul>

Very annoying. Anyway after much searching, I have come to the conclusion that this is caused by a bug in PHP 5.2.4 or later. (Although there seemed to be much debate about which version of 5.2 it started with.) I'm running OS X 10.5 and have PHP 5.2.8 and I definitely have the bug. The main solution involves a patch, which I don't really want to attempt. However, after further digging, I found this workaround (Thank you David Green where ever you are.).


Find the fList function in the classTextile.php file. Replace these two lines of code:



foreach($text as $line) {
$nextline = next($text);

with these two lines of code



foreach($text as $nr => $line) {
$nextline = isset($text[$nr+1]) ? $text[$nr+1] : false;

Fixed me right up. And hopefully, this note will help other Mac OS X folks out there.

Thursday, 28 May 2009

Friday, 17 April 2009

Komodo Edit

Komodo Edit is a free open source editor created by the folks at Active State. These are the people who create the ActivePerl and ActivePython distros.


There are versions of the editor for Windows, Mac OS X, and Linux. It is based of the Komodo IDE which is a much more full featured development environment. Anyway, the editor has a really nice feature set.



  • Supports the concept of projects and allows you to keep multiple projects open at the same time.

  • Tabs for each file you are editing

  • Good syntax hightlighting

  • Built in HTML preview

  • And many more I have not discovered yet.


Definitely worth checking out if you want a cross platform editor.

Wednesday, 15 April 2009

JQuery and PHP Tips

JQuery Logo

Check out this list of 20 useful JQuery and PHP tips via Digg. Haven't checked out all the links, but I look forward to doing that.


Also, check out the new Digg toolbar. Haven't decided if this is good or bad yet. lol. But, it does seem to make browsing thru the stories easier.

Sunday, 29 March 2009

Code Igniter can't Find a Function

PHP Logo
While working my way thru Professional Code Igniter I ran into this error:


Fatal error: Call to undefined function base_url() in /Users/mw119255/si/ci/book/system/application/views/template.php on line 6


Turns out I forgot to enter the suggested helpers and libraries into autoload.php as listed on page 56 of the book. At the moment I haven't loaded the database library. That will happen after I get the basic UI going and then start working on the functions that rely on the db. The book puts in calls to methods that haven't been written yet as you build the UI. As I prefer a more incremental approach with a working application, I am putting in comments in for those calls. The code is uncommented as each method is built.




Wednesday, 4 March 2009

MySQL 101 on OS X - Part II

MySQL Logo

The database is up and running and you have a user account. Now you need to create a database and load it with data. Since I am working on the Professional CodeIgniter book, I will use the database for that book as an example. You can download the claudias_kids.sql file from here. This file will be used to create a claudias_kids database.



Creating the Database


Creating the database is really straight forward. Just use the SQL create database command.


mysql -u monty -psomePass -v -e "create database claudias_kids;"

That is it. Your database has been created.



Loading Data


The next step is to load the database with tables and data. Often when working with a book example, the code to create the test tables and data is provided for you. The data is generally stored in a "dump" file, named after the utility used to export data to a file. A dump file is used in this example. The syntax for loading the data for the claudias_kids database is:


mysql -u student -psomePass claudias_kids < claudias_kids.sql

This command executes all the SQL code in the claudias_kids.sql file. Your database should now be full of tables and data.


To get list of tables, type:


"mysql -u student -psomePass -e "use claudias_kids; show tables;"

To list the contents of the products table, try this:


mysql -u student -psomePass -e "use claudias_kids; select * from products;"

Backing Up the Database


Now you have installed your database. What if you want to back it up after making changes to it? A backup can be created using the mysqldump utility. For example, to backup the claudias_kids database you would use this command.


mysqldump -u root -psomePass --databases claudias_kids > claudias_kids_bk.sql

The command creates a dump file just like the one you used to install the database. The command can be run at any time to save any changes you make to the database.


Well that concludes the posting on MySQL for now. Hopefully that gets MySQL up and going so you can get focused on coding.

Tuesday, 3 March 2009

MySQL 101 on OS X - Part I

MySQL Logo

Well, to get started with my CodeIgniter book, I needed to get MySQL setup again on my machine. So for my own future reference, here are the steps to do that.


Install MySQL


This is pretty easy, go to mysql.com and download the open source server for your operating system. I used the version 5.1.31 for Mac OS X 10.5. There is lots of info on the MySQL site for this if you need it.



Starting and Stopping the Server


The first step is to start the server. On OS X, MySQL is located in the /usr/local/mysql/bin directory. You may want to put this directory in your path to save keystrokes.


To start the MySQL server use this command as root:


sudo bash

/usr/local/mysql/bin/mysqld_safe &

This should get the database server up and running in the background. To test the server to see if it is running, run the mysql client.


mysql

If the server is running you should see something like this:



macpro:~ # mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.31 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

If the server is not running, you will get an error message like this:



ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Eventually, you will want to stop the server. That it done with the following command (also as root):


/usr/local/mysql/bin/mysqladmin shutdown

You can start and stop the server, now it is time to setup a development account.



Creating a User Account


You can actually do all your development using the default root account with no password set. But this is a bad idea. Once you move your code to a hosted web server, you will need to use a username and password to use MySQL. So you might as well get used to it now.


The command to add a user to MySQL is straightforward, but a bit verbose. Here is an example:


mysql


GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost' IDENTIFIED BY 'somePass' WITH GRANT OPTION;


quit

First your start the client and enter an SQL statement. The SQL command creates a superuser account (all privileges are granted) for a user named "monty" connecting from the server named localhost, with a password of 'somePass'. Type quit to exit out of the client.



Alternate Ways of Entering MySQL Commands


Using the text interface of the MySQL client is only one way to enter SQL statement and commands into MySQL. I find the text client to be a bit clunky, so I often enter SQL statements on the command line like this:


mysql -u monty -psomePass -v -e "mysql statement;"

This command executes the SQL statement from the command line and displays the output there. The -v option provides verbose output. The -e option defines the SQL statement to be executed. The advantage of this sort of setup is you don't have to leave you shell prompt to perform database functions. In addition, it is easy to script the command line option. For example, a simple shell script for executing commands might look like this:



#!/bin/bash
mysql -u monty -psomePass -v -e "$1"

Or, if I wanted to script the add user functionality in say Perl, it might look something like this:


   1:#!/usr/bin/perl -w
2:
3:my $userToAdd = "monty";
4:my $userPassword = "somePass";
5:my $hostName = "localhost";
6:
7:# SQL String
8:my $sqlStr = "GRANT ALL PRIVILEGES ON " . '*.*' . " TO '$userToAdd'\@'$hostName' IDENTIFIED BY '$userPassword' WITH GRANT OPTION\n";
9:
10:#print $sqlStr;
11:system "mysql -u root -p -v -e \"$sqlStr\" ";
12:
13:

For someone like me who makes a lot of typo mistakes, this is really helpful.


Well that ends part I. In part II, I will create a database and load a dump file into the database.


For more information see: