Javascript alert box with PHP once per browser session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Javascript alert box with PHP once per browser session

Post by paulng »

I have this javascript that displays an alert box on my home page for users to download a software when click ok, I have been trying to write a session cookie in PHP that display the alert once box per browser session.

here is the javascript code for the alert box

Code: Select all

function getdownload(){
if (confirm("<?php echo("$message"); ?>")) {
window.location.href='../directoty/file.txt';
}
}
and here is my php cookie code that isnt working

Code: Select all

<?  
if (!$_COOKIE['visited']) {  
setcookie("visited", "true"); 
echo "onload=javascript:getdownload();";  
}

?>
can anyone help with a better solution...?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You should set the cookie before any html is outputted.

Code: Select all

<?php
if (!$_COOKIE['visited']) {  
    $show = 1;
    setcookie("visited", "true");
}
?>

<html>
<head>...Javascript and stuff...</head>
<body <?php
if ($show)
{
    print "onload=javascript:getdownload();";
}
?>>...body stuff...</body>
</html>
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post by paulng »

I've tried what you said but it's not working, is not producing any error at the same time the alert message session cookie is not displaying per browser session.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Take a look in the source - is the onload event there on a new session?
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post by paulng »

Yep the onload event is in the source, but still no effect... :?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Well, that's a problem with the javascript.
Let's take a look...

Shouldn't it be:

Code: Select all

print "onload=\"getdownload();\"";
:?:
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post by paulng »

I've tried...still give the same problem let me try to explain everything again
here is my index file

Code: Select all

<?php 
if (!$_COOKIE['visited']) {   
    $show = 1; 
    setcookie("visited", "true"); 
} 
?>
<?php

include("head.php"); ?>
<!---------------- end : head -->

<body <?php  
if ($LANG == "en" || $LANG == "sw" || $show){ 
?>onLoad="getdownload();"<?php  
} 
?>>
<!---------------- start : LAYOUT -->
	<?php include("l/$LAYOUT/php/hplayout.php"); ?>
<!---------------- end : LAYOUT -->
</body>
</html>
the onload javascript function is in the header.php as follow

Code: Select all

function getdownload(){
if (confirm("<?php echo("$message"); ?>")) {
window.location.href='../download/software.exe;
}
}
the alert box is working fine , it just the once per browser session cookie that's not working.... Im sure it something simple it just needs to be figured out... :idea:
Post Reply