Passing shell variables to a webpage

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

This is my assignment as a whole - Use SVG to present certain dynamic (the raw data should change at least once a day) data visually. Consider using any of the conventional ways of that illustrating data (charts, figures, graphs, maps. etc.) or consider more experimental ways of visualizing data such as shown in the work of Edward Tufte or in some of the illustrations of similarity using OWL. Hopefully, your illustration will help guide insights about the data that might not be apparent to someone just looking at the raw textual or numeric data.

Here's the issue. I have both the client and the server side parts done but with place holder values on the client page. I'm having issues figuring out how to get them to talk or rather pass variables to each other.

For simplicity lets work with a simple example and I can then apply that to my project. Say I have a php page or even a simple html/javascript(if possible) page that a user accesses via their web browser. I want to print or alert the variable $A. I have a server side shell script that simply assigns the variable $a to "hello." I want to somehow send that variable to my php/javascript page so that $A is also hello and the alert or echo/print is no longer blank.

Any ideas? I've used POST and GET to send data to a shell script before but its been a one way street with the shell script dynamically building the output page. Can I call the url of my cgi script and have that script then open the url of my webpage with parameters such as 'testpage.domain.com/?A=hello'

  1. Relevant commands, code, scripts, algorithms:

Since I don't really want anyone to do my project for me I've outlined a simple example above so there isn't really any code to post.

  1. The attempts at a solution (include all code and scripts):

I've not tried any actual coding yet because I'm not sure where to start with this but I've Google'd quite a few variations of my question to no avail.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Slippery Rock University, Slippery Rock, PA, United States of America, Dr. David Dailey, CPSC 317

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Does this variable actually exist anywhere, or is it just sitting in some file?

What do you need to be able to do with this variable?

Shell variables only make sense in a shell...

You could export the variable, then have your PHP executed like this:

# variable file

export A="asdf"
#!/bin/sh

. variablefile

/usr/lib/php5.3/bin/php <<EOF
<?php
        printf("variable A is %s\n", getenv("A")); ?>
EOF

exit

But I get the feeling you want to have more control over the variable than that, you need to set and retrieve it when you please, not just when first executed, and do so in a manner safe across several connections.

This is the sort of thing they usually use a database with. The combination of Linux, Apache, Mysql and PHP is so common they usually call it LAMP...

Firstly, thanks for posting I really appreciate any help.

Now at the moment in my simple example the variable is just sitting within that script in my cgi directory. I have another file (at this point its simply a html file) in my public web directory.

Well with my project I need to retrieve a variable from my server script and apply it to the webpage that the user is viewing so that it can dynamically create an svg graph (a rather ghetto graph with simple shapes). But for my simple example I just want to be able to load my html file and have a variable get assigned to the value of a simple variable in my shell script and then either print it or alert it.

Thanks for that advice. I'll look into some of those techniques. Ideally I'm just looking to have the variable set and retrieved when the page is loaded or refreshed. I'm also not too worried about safe transfers with this project but it certainly good to keep that in mind for the future.

Now after talking to some of my classmates I might have found a way that may help. I'm gonna play around with what you've given me here as well as some AJAX and hopefully get somewhere. I'll post back later if I get what I want done.

Just thought of a potentially much simpler way. PHP has a built-in facility which lets you carry around variables between PHP calls. You might just be able to cram it into the $SESSION variable, which PHP stores either on the server or on the user machine with cookies.

See session basic examples

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}

printf("You have accessed this page %d times\n", $_SESSION['count']); ?>

My AJAX lead was helpful. I went to my professor about it and he showed me a few things and I've come up with the code below (with a good bit of help from my professor). I was able to get it to accomplishing what I wanted, then I broke it :wall:. Currently I have it activate as a result of a mouseover and it sorta works but its returning null value now. I must be missing something simple so I would appreciate a second set of eyes.

My javascript code

function createRequestObject() {
        var ro = new XMLHttpRequest();
        return ro;
}

var http = createRequestObject();

function send() {
        http.open('get',"url place holder");
        http.onreadystatechange = doSomething;
        http.send(null);
}

function doSomething() {
        if(http.readyState == 4){
        q=http.responseText
        // this works atm 
            alert(q)
    }
}

My shell script

#! /bin/sh
echo "Content-type: text/html"
echo

$a=happy
echo $a


I do realize this way of going about it may be out of scope of Unix help forum but I do appreciate the help.

Issue solved. I've been working with php so much recently, I didn't realize you don't declare shell variables with a $ sign. The above codes work and accomplish what I needed.