...

View Full Version : Simple Shoutbox



Dfraz
04-23-2008, 09:13 PM
Hey. Here is a shout box I made. Soon when I learn Ajax I'll make it an Ajax shout box.


Sorry I don't have it where it automatically makes the tables :/

Ok here we go.

Go into PHP my admin and make a table called: shoutbox
Then make the fields:

id
username
message






//Here we are making the shoutbox color fields, and tables.

shoutbox.php
Make sure you have the database called "config.php"
<?php include("sfield.php"); ?>
<?php include("config.php"); ?>
<?php //This part is where it shows "Admin CP" and "View Full Shoutbox"
?>
<span class="style1">
<a href="/administratorcp">Admin CP</a> | </span> <a href="viewfullsbox.php">View Full Shoutbox
</a>
| <a href="index.php">Refresh
</a>
</form></td>
</tr>
<tr>
<td height="32" valign="top"><p class="style3"><?php
$result = mysql_query("SELECT * FROM shoutbox order by id DESC LIMIT 5")
or die(mysql_error());
echo "<table border='0'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['username']; echo ": "; echo $row['message'];
echo "</tr><td>";

echo "</hr></tr>";
}

echo "</table>";
?>

&nbsp;</p></td>
</tr>
</table>




sfield.php
//All that does is displays the shoutbox but we need it to add the shouts and display the fields.
<?php require_once("config.php"); ?>
<style type="text/css">
<!--
.style3 {color: #000000}
body,td,th {
color: #000000;
}
-->
</style>

<table width="838" border="1" cellpadding="0" bgcolor="#EAF9E3" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="834" height="32" valign="top"><form name="form1" method="post" action="addshout.php">
<label>
<input name="username" width="335" type="text" id="username" value="Username">
</label>
<label>
<input name="message" width="335" type="text" id="message" value="Message">
</label>
<label>
<input type="submit" name="submit" width="500" id="submit" value="Shout">
</label>



addshout.php

//Now here is what adds the shout:
<script type="text/javascript">
<!--
function delayer(){
window.location = "index.php"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 0)">
<?php require_once("shoutbox.php"); ?>
<?php require_once("config.php"); ?>
<?php
$username = $_POST['username'];
$message = $_POST['message'];

mysql_query("INSERT INTO shoutbox(username, message) VALUES('$username', '$message')")
or die(mysql_error()); ?>
<html>
<head>

That will automatically redirect you to index.php which I'm guessing your s box will be on the index like mine. You may change the location to where you want it.



That wraps up the tutorial of my new shoutbox.
Please rate this thread and give me some reputation if this shoutbox is cool.


Thanks and Good Luck!

Inigoesdr
04-23-2008, 09:53 PM
Sorry I don't have it where it automatically makes the tables :/
If you browse to the table in phpMyAdmin you can click the Export tab, and at least post the schema so people and just run the query.




//Make sure you have the database called "config.php"
<?php include("sfield.php"); ?>
<?php include("config.php"); ?>

You need to post the config.php code(remove your database username/password) so other people can use your script without much effort(especially new people). Also, you don't need to, and shouldn't, keep opening/closing PHP tags after each line like that. You only need to open one, and don't close it until you need to output some HTML or whatever:

<?php
include("sfield.php");
include("config.php");
//This part is where it shows "Admin CP" and "View Full Shoutbox"
?>
<span class="style1">

You have a typo here(and you can use mysql_fetch_assoc() (http://php.net/mysql_fetch_assoc) if you don't need the numerical index of the fields):


while($row = mysqf_fetch_array( $result )) {
You should be sanitizing this input:



$username = $_POST['username'];
$message = $_POST['message'];
See mysql_real_escape_string() (http://php.net/mysql_real_escape_string).
After you fix these things I can delete my post. Thanks for your submission, though. :thumbsup:

Dfraz
04-23-2008, 10:19 PM
Fixed :)

Thanks for telling me that, I guess my mouse slipped or something and put that "f" instead of "l"

Thanks ;)

oesxyl
04-24-2008, 11:45 AM
the presentation is important, is more easy to understand how is made and how can be used, so make a [ php] [ /php] for each file, you can edit the post, :)

filename.php:


// happy coding, :)


also, I have no idea what a shoutbox is, probably I make some but I don't know how it's call, :). Conclusion, try to describe shortly what it is and how can help me, :)

regards

Dfraz
04-24-2008, 05:28 PM
Ok, A shoutbox is like a chat box. But some people call it shout-box and some call it chat box. There both similar.

First see a LIVE preview here: http://www.octi-might.info/index.php

NOW I have it where if you're not logged in it shows up in the shout-box as a guest. If you're logged in it will show as your username.

Administrators and soon moderators will be able to delete shouts and clear all shouts.

oesxyl
04-24-2008, 06:52 PM
it's hard to keep it simple, :)
I see that you post some question to the forum, I try to help you here:

you have users, messages and sessions, so you need IMO, thre tables:

users:
id - uniq, pk and fk
username
rol - admin, moderator, guest and normal users

messages:
id - uniq, pk
user_id, = users.id
message

sessions:
id - uniq, pk
user_id, users.id
state - online/offline

now you can start writing sql query for each operations you have:
- login
- logout
- add user
- delete user
- change user rol
- add message
- delete message
....

any time you add a new operation probably you need to modify the tables structure, add new fields, ....

when you have all this done you can design the interface, pages you need.

I hope this help you and thank you for the explanation about shoutbox.

regards

Dfraz
04-24-2008, 07:14 PM
I'll try soon. But first of all, How can I restrict people from there user control panel if they are not logged in?

I think it's something like:


<? $username
else
php restrict access ?>


Plese help me out.
Thanks

oesxyl
04-24-2008, 07:26 PM
you make the submit button disabled, and don't make it enbaled only if the user is not a guest.



<?php
if(condition to be admin/moderator/normal user){
?>
<input type="submit".... />
<?pgp
}else{
?>
<input type="submit" .... disabled="yes"/>
<?php
}
?>


Also don't forget to check again after submit if the user is admin/moderator or normal user and not a guest.

regards

Dfraz
04-24-2008, 07:28 PM
Woo nice, Thanks.

But also I forgot how to make it where you can restrict people who are not logged in.

If I'm logged in it will be a normal user cp. If I'm logged out it will say "Sorry you must be logged in"

Do you know how to do this?

Thanks

oesxyl
04-24-2008, 07:37 PM
Woo nice, Thanks.

But also I forgot how to make it where you can restrict people who are not logged in.
you must implement the mechanism, that's why I suggest to redesign. If you start from interface it's hard to make it work after.



If I'm logged in it will be a normal user cp. If I'm logged out it will say "Sorry you must be logged in"

Do you know how to do this?

Thanks



<?php
if(condition to be admin/moderator/normal user){
?>
<input type="submit".... />
<?pgp
}else{
?>
<input type="submit" .... disabled="yes"/>
<p>You must be ......</p>
<?php
}
?>


You don't know if a user is admin/moderator or normal user if is not logged in, so you must check that first.

regards

Dfraz
04-24-2008, 07:41 PM
That's not how I've seen it about a year ago.

It was something simillar to what I posted above. But thanks for your help anyways ;)

oesxyl
04-24-2008, 07:51 PM
That's not how I've seen it about a year ago.

It was something simillar to what I posted above. But thanks for your help anyways ;)
the link you post, have a Login/Register, 5th from left to right on menu bar, and probably it store last 5 message somewhere.
If you are not logged in, it display the message as Guest user.


could be something like:


if(!isset($username) || (isset($username) && !loggedin($username))){
$username = "Guest";
}

you must write a function loggedin to check if the user is or not logged.

regards

Dfraz
04-28-2008, 05:57 PM
I got it fixed. Thanks btw.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum