source ... how to initiate once in linux or php


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting source ... how to initiate once in linux or php
# 8  
Old 01-20-2012
Quote:
Originally Posted by Corona688
I'm not sure what to tell you that the examples don't. Ask more specific questions and I'll give more specific answers.
How the PHP code would look like using session? As you said "children get independent copies" whenever you open a shell to run an app.
# 9  
Old 01-20-2012
Session variables are a PHP thing, not a shell one. Calling the setup you have "shell variables" is a bit of a misnomer, anyway. You don't have any shell sitting around holding these variables -- you have a file the values sit in. The shell exists only for a split second as it loads that file and feeds the values into PHP. That's why you have to load it every time -- once the page finishes loading, whatever process was producing the output quits and no longer exists. I don't understand the point of involving the shell at all.

The shell isn't involved at all for PHP session variables. The entire point is to conveniently carry the same variables between different page loads. When the script ends, the contents of $_SESSION are saved somewhere. When the script begins, the contents of $_SESSION are loaded from somewhere. It gives a cookie to the client so it knows which browser has which values. It's sort of analogous to the system you have, but is a feature built into PHP that can tell between different clients and autoloads/autosaves for you.

These values are generally stored on the server itself. You can even control what directory it saves sessions inside by calling session_save_path before you call session_start().

My examples aren't going to be any better than the ones I linked, but if you really want them, here's one written from scratch. It should be a fairly self-contained thing which uses form POST's to tell it what variables to write into $_SESSION.

PHP Code:
<?php   // this MUST happen before any other output is written!
        // The session must start before HTTP headers are sent for
        // cookie-based sessions to work.
        
session_start();

        
// Did we get a POST request with a value to add or update?
        // Change the contents of $_SESSION as directed.
        
if(isset($_POST['key']))
        {
                
printf("<!-- setting %s => %s -->\n"$_POST['key'], $_POST['value']);
                
$_SESSION[$_POST['key']]=$_POST['value'];
        }

        
?>
<body><html>

<table border=1>
        <tr><th colspan=2>Your session ID is <?php echo session_id(); ?></th></tr>
        <tr><th>Key</th><th>Value</th></tr>
<?php   $N=0;
        foreach(
$_SESSION as $k => $v)
        {
                
$N++;   ?>
        <tr><td><?php echo $k ?></td><td><?php echo $v ?></tr>
<?php   }       ?>

        <tr><td colspan='2'><?php echo $N?> keys</td></tr>

        <form method='post'>
                <tr>
                        <!-- Textboxes to input values -->
                        <td><input type='text' name='key'></td>
                        <td><input type='text' name='value'></td>
                </tr>

                <!-- Submit button -->
                <tr><td colspan='2'><input type='submit'></td></tr>
        </form>
</table>

</html></body>

Last edited by Corona688; 01-20-2012 at 02:42 PM..
# 10  
Old 01-21-2012
Thank you for trying to help but actually this is not what I expected or maybe I'm not getting this. Let's go through again:

- the user opens the website,
- when he clicks a link his session starts and runs ONCE script.sh
Code:
#!/bin/bash source app_path/config_file' app_name $* script_name

- next time he clicks a link there is only executed exec(app_name arg script_name) without sourcing anything because his session remembers the initiated environment.

Is it possible to use sessions in this case? If yes then how to code this?
# 11  
Old 01-23-2012
If you think about what you're asking it's immediately apparent. Session variables live in $_SESSION. Environment variables live in $_ENV. $_SESSION gets saved to the session file. $_ENV does not. If you want things saved, you have to copy them into $_SESSION.

I'm still wondering why you ever used environment variables here. You seem to want shell scripts just so you can say you're using shell scripts, not because shell scripts are actually involved in any meaningful way anymore; shell variables simply don't do what you want! Why not do everything in PHP?

PHP Code:
<?php # Include file to set default $_SESSION values

$_SESSION["name1"]="value1";
$_SESSION["name2"]="value2";
$_SESSION["name3"]="value3";

?>
PHP Code:
<?php

session_start
();

# Check for special variable we expect to be set in $_SESSION
# to say we've loaded the file
if(!isset($_SESSION['set']))
{
        require_once(
"session_defaults.php");
        
$_SESSION['set']=1;
}
So if a user gets a blank session, stuff will be autoloaded, otherwise, it'll stay in the session.

You could also load from an external file by these simple means or a more complicated scheme:

PHP Code:
<?php # Include file to set $_SESSION default values


# Load default values from /path/to/file, a file containing
# lines like
# a=b
# c=d
# z=string with spaces and no quotes
$fp=fopen("/path/to/file""r");
while(
$line=fgets($fp))
{
        
$line=rtrim($line"\n"); # Remove newline from end
        
$arr=split($line"="1); # split a=b into array("a", "b")
        
$_SESSION[$arr[0]]=$arr[1]
}

fclose($fp); ?>

Last edited by Corona688; 01-23-2012 at 12:40 PM..
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 01-25-2012
Quote:
Originally Posted by Corona688
If you think about what you're asking it's immediately apparent. Session variables live in $_SESSION. Environment variables live in $_ENV. $_SESSION gets saved to the session file. $_ENV does not. If you want things saved, you have to copy them into $_SESSION.
The more we write, the more I'm convinced that this isn't for that case.

Quote:
Originally Posted by Corona688
I'm still wondering why you ever used environment variables here. You seem to want shell scripts just so you can say you're using shell scripts, not because shell scripts are actually involved in any meaningful way anymore; shell variables simply don't do what you want! Why not do everything in PHP?
I can't do everything in PHP because this application which I call from PHP is totally something different and it uses a special script file (created in a text editor) which knows only that application. The user who is visiting my website sets some parameters and depending on its choices there is created a params file which is used by that script. The final output is a graph or simulation as a gif file which I read with PHP when it's created. I use for that just $_session variable to remember the file name which is deleted after viewing or downloading. So for that I see the sessions are perfect. It's unreasonable to use sessions for non PHP code which is the command "source", since whenever it's called it creates a new empty shell with no configuration. Thanks again for trying to help.
# 13  
Old 01-25-2012
Quote:
Originally Posted by soonic
The more we write, the more I'm convinced that this isn't for that case.
I'm assuming you mean the "not really related to shell" bit and not the bit you actually quoted, since the code you've written agrees with the understanding that env variables aren't persistent, don't autoload, and won't change in unison across programs.

If so, fair enough.
Quote:
I can't do everything in PHP because this application which I call from PHP is totally something different and it uses a special script file (created in a text editor) which knows only that application. The user who is visiting my website sets some parameters and depending on its choices there is created a params file which is used by that script.
Explaining your actual goal from the start would've prevented pages of argument Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

UNIX/Linux inventory - Open Source

Hello guys, I need an open source tool that can list all the softwares installed in my unix/linux servers, the tool should list all the softwares installed and the current version, grouped by the hostname, anybody know any solution for this specific problem? Thanks guys, have a good day! (7 Replies)
Discussion started by: denisloide
7 Replies

2. Linux

Linux open source for admin

Hi, I have created my VM lab on redhat linux, but giving me error after updating new yum repository, its asking me for subscription. I want want switch my redhat linux lab from VM, which linux open source will be best to perform admin commands and tasks?? If possible , please provide iso file... (2 Replies)
Discussion started by: Nats
2 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Programming

How to modify all Makefiles in a source tree into linux environment

Hi Frndz, I am new to the makefile generation. I have a source code which contains makefiles in each sub-directories. Previously we have used the entire source code in Dtbuild environment to get the executables. Now have to change the current Dtbuild environment to Linux build... (1 Reply)
Discussion started by: korraag
1 Replies

5. Shell Programming and Scripting

Linux source code

Unix is written in C. From where u can see the source code of unix or linux (1 Reply)
Discussion started by: pritish.sas
1 Replies

6. SuSE

How do I compile source undfer Suse Linux?

I've been usuing Suse 9.1 & 10 on 2 different machines for a while and have so far relied on ready compiled versions of programs in RPMs in order to install them. I'm unclear what is envolved in compiling from the source code myself. I did a general google on the topic and found some unclear... (3 Replies)
Discussion started by: Mark Ward
3 Replies

7. Linux

source code for linux

Hi , where can i get free source code for LINUX commands , OS , system calls etc. Thanks Naren (2 Replies)
Discussion started by: naren_chella
2 Replies
Login or Register to Ask a Question