473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 40303
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Golf Nut | last post by:
hello all! I would like to create a session class which would transparently handle sessions as well as serialize, encode and compute an md5 hash of all $_REQUEST information. This would...
5
by: Jon Booth | last post by:
I want to write an apsx page that when loaded does not refresh Session.Timeout. It is going to be running in an iframe and refreshing every couple of minutes. example. User has 15 minutes left...
10
by: ANTISPAM_garycnew_ANTISPAM | last post by:
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...
0
by: kingski | last post by:
What is the first event in Global.asax in which I can create/access session in ASP.NET 2.0?
2
by: rodchar | last post by:
hey all, how can i get javascript procedure to execute as the very last thing before the page is rendered? Background: I have a DIV element inside an update panel. I am setting the scrollLeft...
2
by: Divya Khullar | last post by:
Is there a way of updating session variable in JS.
1
by: navanethanmca | last post by:
Hi All, My need is, How can i create a session in side a javascript function, and once check the session, if condition is true then redirect into one page or condition false redirect into...
1
by: =?Utf-8?B?YWxiZXJ0bHk=?= | last post by:
How can I create a new session from the existing session in asp.net when new IE session is opened using javascript's window.open method or user's hyperlink click? I have a web application which...
4
by: ganesh22 | last post by:
Hi, How to Create textboxs using JavaScript in asp.net/c# and how to assign that values to a string?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.