XAMPP and PHP Sessions

February 27, 2009

We assign a php session variable with:

$_SESSION[‘UserID’] = $Username;

and later check it with:

if (isset($_SESSION[‘UserID’]))

but it does not work. We check it out with echo but it is always empty. This is odd as the same code works perfectly on the web server running WAMP.

The WAMP server is php4 and the XAMPP usb stick is PHP 5.  Maybe we should add:

session_write_close();

To the program.

The other possibility is that windows security is to blame – this morning after another reboot for an update the trend anti-virus software quarantined the ATI control panel as it is a risk as well as the Snagit screen capture software. Soon you won’t be able to run any applications on windows…

Found a post where the user edited php.ini and changed line #839  from:

;session.save_path = /tmp

to

session.save_path = /tmp

save and restart php. I don’t think that is the problem as i checked the xampp\tmp directory and found the session file. It has the correct contents:

UserID|s:1:”5″;

so why is the code not working? If time permits i will figure it out but time is short…


Portable Apps now available for OSX

February 20, 2009

Portable Apps for Mac

The portable apps environment we have been using in class is only for windows but FreeSMUG (the free software mac users group) page now has a portable apps environment and downloads for osx.

The page is at: http://www.freesmug.org/portableapps

Most of the applications are the same open source ones such as Audacity, Firefox, VLC etc – which is good because you can have cross-platform coverage. However, there are some very interesting appz like:

Cletx – http://celtx.com/overview.html is media pre-production software for the entire pre-production process – write scripts, storyboard scenes and sequences, develop characters, breakdown & tag elements, schedule production etc.

as well as the mac only appz like ical and ichat.

This will be one to keep an eye on – will have to test it out.


Changing Apache to only use Localhost

February 4, 2009

Some windows configurations will block XAMPP, specifically the Apache web server on port 80. Most people use hardware firewalls on their routers instead of the windows firewall but if you have windows security problems the following may help:

You can change the Apache web server so that it is ‘bound’ only to the loopback network interface known as localhost which has an IP address of 127.0.0.1

To do this:

  1. Insert your XAMPP usb stick
  2. Goto the xampp\apache\conf folder
  3. Select the httpd.conf file and right-click
  4. Select Copy from the menu
  5. Select Edit, Paste or press CTRL C to make a backup copy of the Apache configuration file
  6. Now select httpd.conf, right-click and choose Edit from the menu
  7. Scroll down the file and find the Listen section, which looks like this:

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.

Find the line with Listen 80 and put a # in front of it to turn it into a comment. Type in a new line:

Listen 127.0.0.1:80

and select File, Save from the menu. This means the Apache web server will only listen for requests coming from localhost, the machine you are on – not for requests coming over the network or internet. This is how it should be for a development environment. We were going to cover this in the session on security but it might help those who are having trouble running XAMPP under specific windows setups.


Simple LAMP Application handout change

February 2, 2009

The handout from last class contains instructions on setting up a simple LAMP application. It was written using the older version of phpMyadmin. The new version we installed in class last week has made some changes to the database creation menu.

When you create the modules table you get a simliar 3 column page but the Extra pulldown menu for specifying auto_increment for the id field has been removed.

You now have to select PRIMARY from the Index pulldown menu and place a tick mark in the A_I box.

This is an example where interface changes are not always beneficial. It would be useful if A_I was spelled out as Auto Increment.


Home PC Kills Apache – Skype is the culprit

January 29, 2009

Some students have reported that the XAMPP control panel runs apache and mysql but then the servers quit.

As it turns out this is due to a decision by Skype to use PORT 80 as a secondary PORT for communicating with their Main Server. Port 80 of course is the standard port for web traffic and so Skype and Apache collide.

There are a number of possible solutions:

Start Skype, selecting TOOLS, OPTIONS, Connections and un-check the use of PORT 80 as an alternative port for incoming calls. Then close and re-open Skype. XAMPP should now run.

The second solution is to change the port on which Apache operates.  In a related post we detailed how to edit the httpd.conf file for Apache to can tell Apache to “listen”only on 127.0.0.1 and the same approach can be done to get apache to listen on another port. Change the Listen line from using 80 to 8080 and then restart Apache after saving the configuration file. The only problem with this is instead of typing in the regular http://localhost/ as the address, you’ll have to type in http://localhost:8080/

Some have asked what is a port? We will cover in class next week but you might want to see:

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

If you are using VISTA you probably have worse problems. You might try this video:

http://www.youtube.com/watch?v=owVu-ZpLrZc

best

gord


Drupal on Ubuntu

January 23, 2009

Installing Drupal on Ubuntu – part 1

In the past installing drupal was very difficult as you had to install apache, php,mysql separately and set them up and then install drupal.

I just found out in ubuntu 8.10 there is an entry for Drupal5 in the synaptic package manager. So with the shiny new frankencomputer i decided to give it a whirl.

I selected the package and it install LAMP (minus phpmyadmin, i think) and asks a couple of questions such as setting up a mysql root password etc.

It does not seem to create a menu item or link anywhere. There is an index.html file created in var\www which can be viewed from http://127.0.0.1 to prove LAMP is running but where is the drupal admin page located?

looks like /usr/share where apache2 is, which has a folder called default-site with the index.html ‘it works’ page.

the user/share/drupal5 folder has the index.php page and the install.php page.

Do we have to set up a virtual directory in apache to tell it to use this? Time to RTFM i guess…


XAMPP Tutorial Notes

January 20, 2009

Installing Configuring and Developing With XAMPP

NOTE: The article says to start and stop processes by running start and stop.bat files, this is for installed versions. Since we are using XAMPP as a portable application do not use those files to start and stop the servers. Use the XAMPP control panel.

PHP Hello World

PHP is the programming language in LAMP. We will write a small program as an example using Notepad.

Run Notepad and type the following

<html>
<head>
<title> PHP Hello World Example </title>
</head>
<body>
<p><?php echo “Hello World”; ?></p></body>
</html>

Note that php code:

  • begins with a <?php declaration
  • lines of php code end with a semi-colon
  • and the php code
  • ends with a ?>
  • and that the file must have a .php extension.

Only files with .php are executed, files with .htm or .html are ignored and passed straight to the web server for sending to the user’s browser.

Save the file with a filename of helloworld.php to your USB stick in the xampp\htdocs folder.

Goto your web browser and type: http://localhost/helloworld.php

Create a Database and Insert Data

Click the back arrow of your web browser to return to the XAMPP screen

Under Tools, click phpMyadmin

Type in addressbook as the database and click CREATE

Type colleague as the table name with 5 fields and click GO

When creating the id field select the radio button under the primary key icon

When alll 5 fields are defined click SAVE (we might decide to make email or phone allow null values)

NOTE: this page shows you the actual SQL used to create the table. Copy this code to your session log or diary that you are keeping in Notepad.

It also has a create PHP code button that will provide the code to create the table from PHP.Copy the PHP code to your notepad session log/diary.

Click the back button on your web browser

Click on the INSERT menu item at the top of the page.

Select Restart Insertion and select 5 Rows from the dropdown menu

Add the data as shown in the handout by clicking on the text box in the Value column.

DO NOT add a value for the primary key as this has been set to autonumber.

Entering data into mySQL with myPHPadmin

Entering data into mySQL with myPHPadmin

Once the data for the 5 records has been typed in click the GO button on the bottom of the page to insert all 5 records.

Copy the SQL statement into your diary in notepad.

Note that how the first field inserted is just the name of the field with

no specified value – the database will automatically assign id numbers for each record. You might also want to click CREATE PHP code and copy that code to your diary.

Click the BROWSE menu item at the top of the screen to see the data in the database. Note the id numbers.

Write the PHP program

Switch over to Notepad and create a new document and type the code:

<? php// connect and select a database
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“addressbook”);

// Run a Query

$result = mysql_query(“SELECT * FROM colleague”);
?>

Note this uses 3 php functions that work with mysql:

  • mysql_connect connects the web server to the database and requires a host address, username, and password (our mysql database does not have a password yet – it is blank thus the “”)
  • mysql_select_db selects a database and requres a database name (a database server can have many databases)
  • mysql_query sends a SQL statement to the database server and returns a result

We are also using a variable called $result to hold the results of the query. Variables in PHP are indicated by a dollar sign $. A variable is a container that can hold anything we want (sort of like tupperware!).

Now add the following text to the file after the ?> that ends the php code

<html>
<head>
<title> PHP Address Book Example </title>
</head>

<body>
<h1> Address Book </h1>
<table border=”1″ cellpadding=”3″ cellspacing =”0″>
<tr>
<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Telephone </th>
<th> Email Address </th>
</tr>

This creates a table with column headers corresponding to the fields of our database. What we want to do is to populate the table with the data, thus ensuring an attractive presentation. I changed the values of cellpadding and cellspacing from the handout because it looks nicer.

Now add new PHP code to process the results of the query.

<?php

//loop though all the records

while ($row = mysql_fetch_array($result))

{

echo “<tr>”;

echo “<td>” . $row[‘id’] . “</td>”;

echo “<td>” . $row[‘firstName’] . “</td>”;

echo “<td>” . $row[‘lastName’] . “</td>”;

echo “<td>” . $row[‘telephone’] . “</td>”;

echo “<td>” . $row[’email’] . “</td>”;

echo “</tr>”;

}

This uses a loop that starts with the first record in the result set and continues until there are no more records.

The WHILE condition is TRUE as long as there are records in the result variable. This loop uses another PHP function called

mysql_fetch_array which puts the retrieved data into a list.

The { left squiggly bracket indicates the start of the loop and the right } squiggly bracket indicates the end of the loop.

For each record in the list it the PHP code writes a table row with <TR> and 5 table columns with <TD> and inserts the data from the fields in the database using the $row variable to hold the data.

The period . is used by PHP to add together or concatenate the output, first is the table column <TD> then the actual data followed by the closing tag.

When there are no more records left in the result set then the WHILE condition is now FALSE and the loop ends. Processing then

continues with the next statement.

Now add these lines to finish the program:

//wipe out result data and close connection

mysql_free_result($result);

mysql_close();

?>

</table>

</body>

</html>

We free up the $result variable (because we might re-use the tupperware container to hold something different) and close the database connection.

Note the mysql_close function does not need anything inside the brackets – this closes all database connections.

Now save the notepad file as address.php in the xampp\htdocs folder of your USB stick.

Goto your web browser and type in http://localhost/address.php

You will probably get an error like:

Parse error: syntax error, unexpected

which means you have a typo somewhere, usually a missing bracket, or quote. Check your code, fix it, re-save and try again. When successful you will see:

Display of the PHP program

Display of the PHP program


Welcome LIS 9710 Students

December 9, 2008

LIS 9710 class blog

This is where you can post your session notes, tips, tricks, useful tidbits and other information to share with the class. Each posting gathers credit towards your participation grade. We will be meeting in a computer lab throughout the term but the software will always be with you on your USB stick so you can work almost anywhere. By posting your info to the blog it will always be available to you.

A companion blog is my Ubuntu diary at http://gordnickerson.blogspot.com which is Linux focused but contains related information.


Create your own website, free on WordPress.com.Create your own website.
Start free