469,623 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Create/Update Php Session with Javascript

I am trying to create/update a Php Session with Javascript to confirm
if users have Javascript enabled.

My first thought was to create a Javascript that writes a script tag
referencing a php page, which sets a $_SESSION variable.

javascript.js:

<script type="text/javascript">
<!--//
document.write('<s'+'cript type="text/javascript"
src="javascript.php"></s'+'cript>');
//-->
</script>

javascript.php:

<?php
session_start();
$_SESSION['javascript'] = 'enabled';
?>

I can see that both the javascript.js and javascipt.php files are
executing from the web logs, but when I try to query the
$_SESSION['javascript'] variable from another page $_SESSION is empty.

Is it possible to create/update a Php Session with Javascript?

Might it be done using URLEncoded URL, Javascript XMLHttpRequest, or
something of the like?

Thank you for your assitance.

Respectfully,
Gary

Dec 15 '05 #1
5 39600
AN************************@yahoo.com writes:
I am trying to create/update a Php Session with Javascript to confirm
if users have Javascript enabled.


Uncle Google is your friend:

http://groups.google.com/group/comp....script+enabled

--
Bart

Dec 15 '05 #2
<html>
<head>
<title> Best way I know is... </title>
</head>
<body>
<?php
if(isset($_GET['js']))
{
echo "JS is on";
//
// Code for JS on
//
}
else
{
echo "
<form name='testjs'>
<input type='hidden' name='js' value='on'>
</form>
<script>
<!--
document.testjs.submit()
//-->
</script>
";
echo "JS is off";
//
// Code for js off
//
}
?>
</body>
</html>

Dec 15 '05 #3
Try doing something like this in javascript.php to see if you have the
right session id:

echo "alert('$session_id');";

Dec 16 '05 #4
Here's the solution:

/includes/meta.php

<?php session_start(); if (!$_SESSION['javascript']) { ?><script
type="text/javascript" src="/js/javascript.php"></script><?php } ?>
/js/javascript.php

<?php
session_start();

if (($_COOKIE['PHPSESSID']) || ($_GET['PHPSESSID'])) { ?>
<!--//
document.write('<s'+'cript type="text/javascript"
src="/includes/javascript.php?PHPSESSID=<?php if
($_COOKIE['PHPSESSID']) { echo $_COOKIE['PHPSESSID']; } else { echo
$_GET['PHPSESSID']; } ?>"></s'+'cript>');
//-->
<?php } ?>
/includes/javascript.php

<?php
session_id($_GET['PHPSESSID']);
session_start();

$_SESSION['javascript'] = 'enabled';
?>

The meta.php script is a global include file that initiates a request
to js/javascript.php if the $_SESSION['javascript'] variable is not
found.

The js/javascript.php script then executes a javascript initiated
request to the includes/javascript.php script using the PHPSESSID
variable and updates the user's php session $_GET['PHPSESSID'] with
$_SESSION['javascript'] = 'enabled';

It does have a short coming in that the first loaded page will not show
that $_SESSION['javascript'] is set. Only pages loaded after the
initial page will be usefull.

Hope this helps someone else.

Respectfully,
Gary

Dec 16 '05 #5
<body>
<script language=javascript>
window.location.href="mypage.php?js=on";
<script>
<noscript>
<a href="mypage.php?js=off">click here to continue</a>
<noscript>
</body>
I am new to sessions, do somewhere in the code you wuold need to insert the
session stuff.

"Ian B" <ia********@gmail.com> wrote in message
news:11********************@g44g2000cwa.googlegrou ps.com...
<html>
<head>
<title> Best way I know is... </title>
</head>
<body>
<?php
if(isset($_GET['js']))
{
echo "JS is on";
//
// Code for JS on
//
}
else
{
echo "
<form name='testjs'>
<input type='hidden' name='js' value='on'>
</form>
<script>
<!--
document.testjs.submit()
//-->
</script>
";
echo "JS is off";
//
// Code for js off
//
}
?>
</body>
</html>

Jan 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Golf Nut | last post: by
5 posts views Thread by Jon Booth | last post: by
10 posts views Thread by ANTISPAM_garycnew_ANTISPAM | last post: by
2 posts views Thread by rodchar | last post: by
2 posts views Thread by Divya Khullar | last post: by
1 post views Thread by =?Utf-8?B?YWxiZXJ0bHk=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.