PDA

View Full Version : Please Help Me With This Code!!! :( (ALERT CODE)



jad9321
06-18-2006, 10:50 PM
Hello guys.

I was wondering if anyone knows of a code that allows admins to send pop up alert messages to the users that are currently viewing a web page. Sourta like a Java script pop up alert, but one that admins can send them when ever they want. I'm not even sure if there is a such script but its worth a try. :)

Thanks in advance,
Joe

Twey
06-18-2006, 10:55 PM
There isn't, and it's not possible. :)
Well -- using AJAX and some server-side scripting, it probably is. But it isn't with pure Javascript.

jad9321
06-19-2006, 12:57 AM
Ok,

Do you know of any tutorials that can teach me how to do that with AJAX and serverside scripting???

Thanks,
Joe

djr33
06-19-2006, 07:17 AM
I have no idea about your skill level, so please don't be insulted when I say this.... AJAX is pretty complex. It involves knowing both javascript and php well, and then getting them to work together. It isn't a thing you could find a quick little tutorial on, for example, but rather something you could write many books about.
Just google for some examples/tutorials and see if it starts making sense.
Just ask yourself... is this really worth it? If so, then dive right in.

An easier way would be to use just server side coding.
You could store a message somewhere, and only display the alert (using php to control the html/javascript output... this is easy) if you wanted to display the alert.
It wouldn't be live, but it would display upon refreshing.
Kinda like the PM system here.


Anyway, there are a lot of ways of doing it... depends how much work/learning you want to do.

jad9321
06-19-2006, 02:50 PM
I know PHP and I know a LITTLE bit of Javascript... I reallly need help. If you can maby work this out for me I will give you Major Credit on my website...

mwinter
06-19-2006, 08:32 PM
I have no idea about your skill level, so please don't be insulted when I say this.... AJAX is pretty complex. It involves knowing both javascript and php well, and then getting them to work together.You forgot to mention HTTP. If someone plans on writing code using an XMLHTTP request object, they should thoroughly understand client and server behaviour as you're essentially writing part of the former.

Then again, anyone attempting to control server behaviour should also have a very good understanding of the HTTP protocol (and it's amazing just how many people don't).



I know PHP and I know a LITTLE bit of Javascript... I reallly need help.Even if you followed Twey's suggestion, it would require polling the server, and that's not a nice thing to do either to the server, or the user.

The server cannot send anything of its own accord; the client must initiate a request. The solution you want is simply not practical over HTTP.

Mike

djr33
06-20-2006, 05:30 PM
Yeah, HTTP too, Mike.


We talked on AIM last night, and here's the code I suggested. This will happen on refreshing, based off a text file... upload with content if you want it displayed, leave the file blank or don't upload if you don't want it displayed. Quite simple, and I suggested adding in cookies. Anyway, here's a start:
PUT THIS AT THE TOP OF YOUR PAGE, EVERY PAGE YOU WANT TO HAVE THE ALERT ON:
<?php
$alert = @file_get_contents('alert.txt');
if ($alert != "") {$alert = " alert('".$alert."');";}
?>
AND THIS FOR YOUR BODY TAG:
<body<?php echo $alert; ?>>

Twey
06-20-2006, 06:02 PM
Uuuhhh...

Example output would be:
<body alert('You've got mail!');>That's not valid HTML, and the quotes would break the script even if it were. Not only that, what if s/he wanted to move the file? That's a lot of pages to edit. I'd suggest putting this in a seperate file:
<?php
$alert = addslashes(file_get_contents('alert.txt'));
if ($alert !== '') $alert = " onload=\"window.alert('$alert');\"";
?>... then including it:
<?php require_once('alert.inc.php'); ?>
<body<?php echo($alert); ?>>@ should never be used. It makes debugging a lot harder -- you have to go through the code removing all those @s to get to the bottom of a problem. If you want to suppress error reporting, do it globally with
error_reporting(E_NONE);

djr33
06-20-2006, 11:29 PM
Ok, wow, I was tired.

The alert thing needs to be replaced with:
onLoad="alert(....

My bad. :p

jad9321
06-21-2006, 12:43 PM
Okkk,

That code dosen't work. http://smilealert.ueuo.com/alert.php I need that fixed than I need the knowledge of the cookie. I want to to block Only a message but once the message is displayed it shouldn't show up again. But if I change the alert.txt it should show the new message.

Twey
06-21-2006, 04:32 PM
"Doesn't work" isn't very helpful. :) Please explain exactly what you did and what it didn't do that you expected it to do, with a link to a demo exhibiting the problem if possible.

jad9321
06-21-2006, 08:21 PM
Ok,

I placed the code that you guys gave me into a php file. Uploaded it to a web host. Than made a txt file that said "testing" and uploaded that. I was expecting it to have a pop-up alert that says "testing" But instead it just showed me the code in the browser. I did put a link to the demo in the last post by me. Well anyway heres the link again: http://smilealert.ueuo.com/alert.php

Twey
06-21-2006, 08:30 PM
Looks as though it's not being parsed as PHP.

jad9321
06-21-2006, 09:49 PM
I thought it might be my web hosting so i tried it on another Host.
It still didn't work. Actually it didn't even show the coding like it did on the other server. Take a look at it: http://smileyoureahabbo.freepgs.com/testing/alert/test.php And this server DEFINITELY Identifies it as php. It said it in the control panel.

Twey
06-21-2006, 10:14 PM
That's right -- it's not meant to do anything.
You now need to include it on another page, as I showed you in my example.

jad9321
06-22-2006, 01:55 AM
OK,

That code works now that you said that. Now I need something that allows me to have a message come up once. For example, My message is "hello" that will only show once. Once I change the alert.txt file to lets say... "bye" the New message should show but only once... And so on. Like djr33 said this probably be done with a cookie. Anyone want to help? Cause I CANT do cookies.

djr33
06-22-2006, 06:43 AM
You really gotta start looking at php.net for info on this stuff. I promise you can learn if you try. It isn't that hard, and will really help in the end.


<?php
$file = 'alert.txt';
$alert = addslashes(file_get_contents($file));
$cookie = $alert.filemtime($file);
if ($_COOKIE['alert'] != $cookie) {
setcookie('alert', $cookie);
if ($alert !== '') $alert = " onload=\"window.alert('$alert');\"";
}
?>

This does the cookie thing, only executes the operation if the cookie doesn't equal the value in the file, AND it will work with duplicate messages, because it gets the last modified time for the file (filemtime(...)) and adds that to the cookie.
I guess you could just use filemtime.
Maybe that's the better route.
Replace:
$cookie = $alert.filemtime($file);
with:
$cookie = filemtime($file);

Twey, what would you think here?


Actually, now that I'm thinking about it... I'd say to replace it... it'll be a shorter cookie, and the alert might be a really long cookie, which isn't a good thing.

Twey
06-22-2006, 12:59 PM
The filemtime will serve fine, there's no need to save the alert as well.

jad9321
06-22-2006, 03:34 PM
Its not working. The same message comes up. (with the filetime thingy) Demo: http://smileyoureahabbo.freepgs.com/Djjoe/alertpage.php

djr33
06-22-2006, 08:39 PM
You're using this as the included page:

<?php
$file = 'alert.txt';
$alert = addslashes(file_get_contents($file));
$cookie = filemtime($file);
if ($_COOKIE['alert'] != $cookie) {
setcookie('alert', $cookie);
if ($alert !== '') $alert = " onload=\"window.alert('$alert');\"";
}
?>

And the same old code on the real page?

jad9321
06-22-2006, 10:31 PM
Its not doing anything.

djr33
06-22-2006, 10:31 PM
change the end to this... simpler...


if ($_COOKIE['alert'] != $cookie && $alert !== '') {
setcookie('alert', $cookie);
$alert = " onload=\"window.alert('$alert');\"";
}
else {
$alert = "";
}
?>

EDIT: and added one more thing... might help.

jad9321
06-22-2006, 11:13 PM
As djr33 said on AOL I duplicated the file and made it .phps

Take a look.

http://smileyoureahabbo.freepgs.com/Djjoe/alert.inc.phps

djr33
06-23-2006, 12:04 AM
Ah.

The problem:

<?php
$alert = addslashes(file_get_contents('alert.txt'));
if ($alert !== '') $alert = " onload=\"window.alert('$alert');\"";
?>

Take it out.

You've put the code twice... one new version, one old version, and the old one ignores the cookies... yeah.

jad9321
06-26-2006, 05:53 PM
Wonderful. It works. Thanks a lot guys.

jad9321
06-27-2006, 03:39 PM
Is there anyway that you can make the cookie expire after 5 mins? Because My admins like to send the same message twice.

Twey
06-27-2006, 04:01 PM
If you're using the filemtime, the same message can be sent twice just by touching the file.

jad9321
06-28-2006, 05:17 PM
Huh? :\

Twey
06-28-2006, 05:59 PM
On *n?x, just:
$ touch <filename>On Windows, I think you have to manually open the file and save it again.

djr33
06-29-2006, 07:03 AM
The basic point is that filemtime() gets the modification time (the MTIME of filemtime) of the file... when it was last changed.
If you change it (or don't just pretend to by resaving), it will send it again.


Also, I've decided you should add something to the code:

<?php
$file = 'alert.txt';
$alert = addslashes(file_get_contents($file));
$cookie = filemtime($file);
if ($_COOKIE['alert'] != $cookie && $alert !== '' && time()-$cookie<900) { //THE LAST IF IN THIS LINE
setcookie('alert', $cookie);
$alert = " onload=\"window.alert('$alert');\"";
}
else {
$alert = "";
}
?>

The last if in the line marked "//...THIS LINE" (Take out the comment... just to let you know what I added) will make the alert only popup if the modification time of the file is less than 900 seconds earlier than the current time. Note that 900 seconds is 15 minutes.... so... use what you want, like 300 for 5 minutes or 60 for a minute.
That way, a really old alert won't pop up for a new user.
You don't need to use this if you always want the last alert to popup for everyone, but it makes sense.

(Also, cookies are deleted over time and that will stop the same message from popping up twice if the message isn't changed for a while and a user, who has deleted their cookies, returns to your site (after 15 minutes).)


**Note that you must add the end bracket } at the bottom as well.