For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > Create/Update Php Session with Javascript









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Create/Update Php Session with Javascript
ANTISPAM_garycnew_ANTISPAM@yahoo.com

2005-12-15, 7:55 am

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

Bart Van der Donck

2005-12-15, 7:55 am

ANTISPAM_garycnew_ANTISPAM@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...ascript+enabled

--
Bart

Ian B

2005-12-15, 7:55 am

<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>

Chung Leong

2005-12-15, 6:57 pm

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

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

ANTISPAM_garycnew_ANTISPAM@yahoo.com

2005-12-15, 9:55 pm

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

Jim Michaels

2006-01-19, 6:58 pm

<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" <ianbambury@gmail.com> wrote in message
news:1134647910.968633.6220@g44g2000cwa.googlegroups.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>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com