Create/Update Php Session with Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ANTISPAM_garycnew_ANTISPAM@yahoo.com

    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

  • Bart Van der Donck

    #2
    Re: Create/Update Php Session with Javascript

    ANTISPAM_garycn ew_ANTISPAM@yah oo.com writes:
    [color=blue]
    > I am trying to create/update a Php Session with Javascript to confirm
    > if users have Javascript enabled.[/color]

    Uncle Google is your friend:



    --
    Bart

    Comment

    • Ian B

      #3
      Re: Create/Update Php Session with Javascript

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

      Comment

      • Chung Leong

        #4
        Re: Create/Update Php Session with Javascript

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

        echo "alert('$sessio n_id');";

        Comment

        • ANTISPAM_garycnew_ANTISPAM@yahoo.com

          #5
          Re: Create/Update Php Session with Javascript

          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($_GE T['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

          Comment

          • Jim Michaels

            #6
            Re: Create/Update Php Session with Javascript

            <body>
            <script language=javasc ript>
            window.location .href="mypage.p hp?js=on";
            <script>
            <noscript>
            <a href="mypage.ph p?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@gma il.com> wrote in message
            news:1134647910 .968633.6220@g4 4g2000cwa.googl egroups.com...[color=blue]
            > <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>
            >[/color]


            Comment

            Working...