By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,729 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

Create/Update Php Session with Javascript

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
<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

P: n/a
Ian B wrote:
<html>
<head>
Not Valid. <URL:http://validator.w3.org/>
<title> Best way I know is... </title>
</head>
<body>
<?php
if(isset($_GET['js']))
{
echo "JS is on";
//
// Code for JS on
//
So if I happen to access the resource with "...?js=foo" you assume
client-side script support is enabled? What ridiculous kind of
"detection" is that? You should have at least used $_POST.
}
else
{
echo "
?>non-PHP content<?php

is faster and less error-prone.
<form name='testjs'>
<input type='hidden' name='js' value='on'>
</form>
<script>
Not Valid.
<!--
Unnecessary and potentially harmful.
document.testjs.submit()
Ohh yes, if that works (e.g. there is no submit button), users will be happy
when something is submitted without their interaction. Really. NOT.
//-->
Unnecessary and potentially harmful.
</script>
";
echo "JS is off";
No, it could as well be that such abuse of client-side scripting is blocked
by the UA.
[...]


There is no alt.php or comp.lang.php anymore (there is comp.lang.php.misc
for example) and Java != JavaScript. Furthermore, you should not crosspost
across top-level hierarchies (here: alt.* and comp.*).
X-Post reduced to clj.p and cl.js, F'up2 cl.js

PointedEars
Dec 15 '05 #4

P: n/a
Thomas 'PointedEars' Lahn wrote:

There is no alt.php or comp.lang.php anymore


Umm - since when?

http://groups.google.com/group/alt.php shows posts being made to
alt.php as of December 15 - isn't that today?

It also shows up on my news server...

Dec 15 '05 #5

P: n/a
Tony wrote:
Thomas 'PointedEars' Lahn wrote:
There is no alt.php or comp.lang.php anymore
Umm - since when?


Since now[1] ;-)
http://groups.google.com/group/alt.php shows posts being made to
alt.php as of December 15 - isn't that today?
Seriously:

Google Groups (formerly DejaNews) always was an archive that was independent
of Usenet. And they never removed any dead group (at least they did not
the last time I checked which was before the beta). Therefore, people
using Google Groups often post to dead groups and wonder why nobody or
only Google posters reply.

Since the beta, the independence from Usenet has increased as Google
is no longer simply mirroring Usenet, it allows users to create groups
that are not found on any news server.

On the other hand, alt.ALL is a very ... volatile hierarchy, as virtually
anyone can send newgroup and rmgroup.
It also shows up on my news server...


Which is?

It could be that my news server (news.arcor.de) simply does not have this
newsgroup. But since it appears to have much of the rest of alt.* (6201
groups here), I think it is more likely that your admin either ignored the
rmgroup or let the news server follow all control messages for this group
-- the difference between a good maintained news server and a bad one.

Hard to say which one applies, but I tend to the latter because of

a) the 23 latest postings

GMT domain of injecting news server
-------------------------------------------------
20:05 ntli.net
20:03 supernews.com
18:39 googlegroups.com
12:18 individual.net
10:50 telia.net
10:28 googlegroups.com
09:37 individual.net
08:59 googlegroups.com
03:14 googlegroups.com
02:37 googlegroups.com
01:58 comcast.com
00:07 ntli.net
2005-12-15 00:04 comcast.com
23:28 googlegroups.com
22:47 newsguy.com
21:53 googlegroups.com
21:46 euronet.nl
17:23 newsguy.com
07:44 btopenworld.com
2005-12-14 05:30 telus.net
23:46 googlegroups.com
22:43 individual.net
2005-12-13 22:42 news:43*********************@news.euronet.nl

(That was from Google's first page, sorted by date. I had to
dig that out _manually_ and there is XNAY, so please don't nail
me on that :))

This does not strike me as a widely distributed, active newsgroup (I
included the message ID of one posting so that you see where the action
really is):

- If you watch the times, there are large gaps up to 10 hours between
postings.
- If you watch the injecting servers and User-Agent headers, you see
almost only Web interfaces.
- If you watch the postings, what is posted there are mostly RTFM questions
often crossposted to other groups.
- And if you watch the threads, there is none of those postings with more
than two replies, highly unusual for a technical discussion group.

Compare comp.lang.php:

19:25 cistron.nl
19:12 cistron.nl
19:05 ntli.net
18:28 googlegroups.com
16:25 btopenworld.com
16:16 googlegroups.com
16:02 xs4all.nl
15:58 googlegroups.com
15:03 googlegroups.com
15:01 xs4all.nl
14:34 googlegroups.com
13:04 easynews.com
12:56 googlegroups.com
12:48 googlegroups.com
12:46 googlegroups.com
12:20 skynet.be
12:18 individual.net
12:01 googlegroups.com
11:58 googlegroups.com
11:14 googlegroups.com
11:06 googlegroups.com
10:53 googlegroups.com
2005-12-15 10:53 btopenworld.com

Google Groups is the majority, of course, but watch the times.
Threads have usually 4 and more postings.

Compare comp.lang.javascript:

19:59 prodigy.com
19:20 googlegroups.com
19:20 xs4all.nl
19:14 googlegroups.com
18:36 googlegroups.com
18:28 googlegroups.com (the precursor of this posting)
18:08 googlegroups.com
18:07 newsguy.com
17:43 comcast.com
17:35 arcor.de (me)
17:31 newsguy.com
17:19 arcor.de (me)
17:16 googlegroups.com
17:12 arcor.de (me)
17:04 googlegroups.com
16:57 googlegroups.com
16:55 arcor.de (!me)
16:50 demon.net
16:47 arcor.de (me)
16:47 xs4all.nl
16:22 googlegroups.com
16:19 arcor.de (me)
16:19 blueyonder.co.uk
16:11 googlegroups.com
16:01 googlegroups.com
15:30 newsguy.com
15:25 googlegroups.com
14:46 googlegroups.com
14:45 2005-12-15 iinet.net.au

(Because I am posting here, I added some more data than above.)

Threads have usually 4 and more postings. Despite Google Groups is the
majority, there are many different newsservers. And watch the times.

Now that strikes me as being a very active group :)
b) due to the ... volatility of ("chaos in" are the better words ;-)) alt.*,
several server admins just gave up and ignored a newgroup in this TLH for
which they just received a rmgroup before.

See also
<URL:http://groups.google.de/group/alt.config/browse_frm/thread/382424024c6cdc4d/563804dc0a2e62ed?lnk=st&q=rmgroup+alt.php&rnum=4&h l=de#563804dc0a2e62ed>

and other discussions. Therefore, I think it is best to use only the
appropriate hierarchy and newsgroup for PHP, comp.lang.php, and consider
alt.php and the like obsolete.

[1] So as for comp.lang.php, I stand corrected; I mixed that up with
de.comp.lang.php, which was splitted a while ago.
PointedEars
Dec 15 '05 #6

P: n/a
Try doing something like this in javascript.php to see if you have the
right session id:

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

Dec 16 '05 #7

P: n/a
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 #8

P: n/a
JRS: In article <22****************@PointedEars.de>, dated Thu, 15 Dec
2005 21:56:56 local, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Tony wrote:
Thomas 'PointedEars' Lahn wrote:
There is no alt.php or comp.lang.php anymore


Umm - since when?


Since now[1] ;-)


No. Both are recognised by the Demon news server, and Demon are
generally good at getting the newsgroups list right, both for additions
and removals.

In your urge to dominate this newsgroup you have formed the habit of
answering without sufficient care about getting things right. This
wastes more time than you might otherwise hope to save.

Stick to things where you have a reasonable chance of being right, and
check your facts and code before posting. And give up this dictator
complex - there's probably still wood to chop at Doorn.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Dec 16 '05 #9

P: n/a
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn <Po*********@web.de> posted :
Tony wrote:
Thomas 'PointedEars' Lahn wrote:
There is no alt.php or comp.lang.php anymore
Umm - since when?

Since now[1] ;-)


No. Both are recognised by the Demon news server [...]


Obviously you are unable to recognize humor or even subtile attempts at
that. I pity you.
PointedEars
Dec 17 '05 #10

P: n/a
<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 #11

This discussion thread is closed

Replies have been disabled for this discussion.