LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2006, 01:25 PM   #1
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Rep: Reputation: 30
PHP conference room problem


Hi all,

I am trying to make a conference room in PHP, using a text file to store all inputted information (I can't download the OWASP filters to prevent SQL injection, so I'm going for a text file instead of MySQL for storage). However, I have encountered a problem.

Here is the current code in its entirity:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xlmns="http://www.w3.org/1999/xhtml">
<head>
<title>Conference Room</title>
<style type="text/css">
body {margin: 0 0 0 0; padding: 0 0 0 0; background: #333; font-family: Helvetica,Arial,sans-serif;}

#top {
    height: 60px; width: 100%;
    }

#form {
    position: absolute;
    left: 50%; margin-left: -200px;
    }

#bottom {
    height: 100px; width: 100%;
    font-size: 13px;
    color: yellow;
    }
    
#footer {
    font-size: 10px;
    color: white;
    margin-top: 30px;
    }

table, table * {
    border-width: 1;
    font-family: Helvetica,Arial,sans-serif;
    color: white;
    }

th {text-align: left; border-bottom: 1px dashed #FFF;}
th#name {width: 250px;}
th#date {width: 150px;}
th#mess {}

</style>
</head>
<body>
    <div id="top" align="center">
        <img src="images/top.png" style="left: 50%;" />
    </div>
    <div id="middle">
            <?php
            
            
// If a user has submitted a post, we want to:
            // 1. Validate it;
            // 2. Strip unwanted HTML tags;
            // 3. Make sure the name isn't too long;
            // 4. Add the name and message to our file - text.txt.
            
if($_POST['submit']) {
                
// 1. Validate it - let's make sure that all the form inputs were filled in.
                
if(!$_POST['name']) {
                    die(
'Error! No name entered.');
                }
                if(!
$_POST['message']) {
                    die(
'Error! No message entered.');
                }
                
                
// 2. Strip unwanted HTML tags.
                // For more information about the strip_tags function, see this page: http://php.net/manual/en/function.strip-tags.php
                
$name strip_tags($_POST['name'], '');
                
$message strip_tags($_POST['message'], '');
                
                
// 3. Make sure name is not too long.
                // We will use the strlen() function to count the length of the string.
                
$name_length strlen($name);
                if(
$name_length 50) {
                    die(
'Error! Your name was too long; names must be less than 50 characters.');
                }
                
                
// 4. Append the data to our file.
                // There will be three parts added. They will be:
                //   - the name;
                //   - the message;
                //   - the date.
                // In that order.
                // For more information about the date function, see this page: http://php.net/manual/en/function.date.php
                
$date date("h:i A dS M");
                
// This will produce a date in the format:  11:02 25th Aug
                
                // Set the data.
                
$input $name "<" $message "<" $date "<";
                
// Append the data to the file. If it doesn't work, exit with an error message..
                
if($handle fopen("text.txt""a") == FALSE) {
                    die(
'Error! Could not open file.');
                }
                
                if(
fwrite($handle$input) == FALSE) {
                    die(
'Cannot write to text.txt');
                } else {
                    echo 
'Thank you for your post<br />' .
                    
'<a href="index.php">View the conference room</a>';
                }        
                
                
fclose($handle);
            
            
// If they HAVEN'T submitted a post, we want to:
            // 1. Show the latest posts;
            // 2. Show the post form.
            
} else {
                
// 1. Show the latest posts.
                // Open the file with read permissions only.
                
$handle fopen("text.txt""r");
                
// Get all the text in the file, and shove it into the array $text, separating it into different elements in the process.
                
$text explode("<"fgets($handle));
                
                
// Now, let's organise all the posts neatly.
                // Set the table's details, and shove in the headers.
                
echo '<table border="0" style="width: 95%;" align="center">' .
                
'<tr>' .
                
'<th id="name">Name</th>' .
                
'<th id="date">Date</th>' .
                
'<th id="mess">Message</th>' .
                
'</tr>';
                
// Now, let's get the posts in.
                
$k 0;
                for(
$i 0$i count($text); $i 1) { 
                    switch(
$k) {
                        case 
0:
                            echo 
'<tr>';
                            
$k++;
                            break;
                        case 
1:
                            
$k++;
                            break;
                        case 
2:
                            echo 
'</tr>' .
                            
'<tr>';
                            
$k 0;
                            break;
                        default:
                            break;
                    }                    
                    echo 
'<td>' $text[$i] . '</td>'
                    
$i++;
                } 
                
                echo 
'</table>' .
                
'</div>' .
                
'<br />' .
                
'<div id="form">' .
                
'<form action="index.php" method="post">' .
                
'<span style="font-size: 13px; color: yellow; margin-left: 6px;">' .
                
'Name: </span>' .
                
'<input name="Name" type="text" style="margin-left: 43px; padding: 4px;" />' .
                
'<br /><br />' .
                
'<span style="font-size: 13px; color: yellow; margin-left: 6px;">' .
                
'Text: ' .
                
'<textarea name="message" rows="5" cols="40" style="margin-left: 50px; padding: 4px;" /></textarea></span>' .
                
'<br />' .
                
'<input type="submit" value="Submit" style="margin-top: 5px; margin-left: 379px;" />' .
                
'</form>' .
                
'</div>';
            }
            
?>
        
    </div>
</body>
</html>
Ok. Now, first of all, the form does not append the data to the file properly, and I'm not sure why - but I think we should leave this problem till later.

At the moment, we need to create the file text.txt in the same directory as the above index.php. In our sample text.txt file, let's pretend that some people have entered information, so that text.txt contains:
Quote:
Max<Thu 28th May<Hey everyone! How are you all? xD<John<Fri 29th May<Great thanks! How about you mate?<Max<Thu 28th May<Brilliant thanks! So how's it going around here?<
Now, let's try visiting index.php in our browser.. I was using Firefox, and it returned this:
click for screenshot

What's the actual source code that the script in index.php is producing? Here it is:
Code:
<table border="0" style="width: 95%;" align="center">
	<tr>
		<th id="name">Name</th>
		<th id="date">Date</th>
		<th id="mess">Message</th>
	</tr>
	<tr>
		<td>Max</td>
		<td>Thu 28th May</td>
	</tr>
	<tr>
		<td>Hey everyone! How are you all? :D</td>
	<tr>
		<td>John</td>
		<td>Fri 29th May</td>
	</tr>
	<tr>
		<td>Great thanks! How about you mate?</td>
	<tr>
		<td>Max</td>
		<td>Thu 28th May</td>
	</tr>
	<tr>
		<td>Brilliant thanks! So how's it going around here?</td>
	<tr>
		<td>
	</td>
</table>
As you can see, some of the <tr> tags aren't closed.. And a new <tr> is begun right before it is meant to. Basically, what I am trying to do is add a <tr> tag, then add three <td> tags containing the content of three of the array's elements, then add a </tr> tag, and then continue for as many messages as there are. But, for some reason (yes, point at me all you want - I am a PHP newbie ), it is not happening.

Any help would be greatly appreciated!

Last edited by J_K9; 05-27-2006 at 01:28 PM.
 
Old 05-27-2006, 02:09 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Your case 2: closes the table row before the details are written out, I'd change it by removing the switch entirely and just adding a couple of if statements:

PHP Code:
$k=0;
for(
$i 0$i count($text); $i++)
{
   if(
$k == 0)
      echo 
'<tr>';
   echo 
'<td>' $text[$i] . '</td>';
   if(
$k == 2)
   {
      echo 
'</tr>';
      
$k 0;
   }
   else
      
$k++;

 
Old 05-27-2006, 02:17 PM   #3
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
Ah, of course! Thank you.

Any ideas about the input to the file? I think my syntax may be wrong, so I'm going to check the PHP manual now..

Thanks
 
Old 05-27-2006, 02:29 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What error are you getting, or what is happening?

My guess is that the browser doesn't have permissions to write to the file. You can check that with the is_writable function.
 
Old 05-27-2006, 02:35 PM   #5
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
I don't get an error - when I enter the fields and click 'Submit', the text just disappears (the page seems to reload) but it isn't any different. I tried reloading with a cleared cache just in case, and it's definite - there has been nothing added to the text.txt file (I also verified this by opening the file).

I'll check out the is_writable function.. Is there anything else I should be looking for? (e.g. Is my input code correct?)

Thanks again!
 
Old 05-27-2006, 02:49 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It looks okay, which is why I think that it is something to do with file permissions
 
Old 05-27-2006, 03:55 PM   #7
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
Hmmm... I added this bit of code:
Code:
if(is_writable('text.txt')) {
	echo 'Writable';
} else {
	echo 'Not writable';
}
The output: 'Writable'. So, it looks alright from the permissions side (I'd tell you the exact permissions, but I'm not on Linux at the moment (sadly) -- have to be on Windows for school ).

I wonder what could be causing the problem..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Video conference edgjerp Linux - Software 0 01-29-2005 02:10 AM
New to PHP and Postgressql. Is this a PHP problem or a sql problem. Please help sendas4 Linux - General 2 11-05-2004 01:54 PM
Apache php index.php problem neurotic Linux - Software 3 11-18-2003 06:02 PM
More Room on / Difficult Linux - Newbie 15 07-10-2002 03:44 PM
how do I create a chat room using php Bheki Programming 3 02-05-2002 04:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration