p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   passing variables from PHP to Perl (http://p2p.wrox.com/showthread.php?t=15727)

joconnor July 21st, 2004 06:52 AM

passing variables from PHP to Perl
 
hi, i'm pretty new to PHP, only started learning it a couple of weeks ago because i'm doind an internship as part of my course in college.
my problem is that i need to pass to variables that get their values from textfields to a Perl file. i have absolutly no idea hhow to do it or even where to begin so ANY help would be greatly appreciated.
thanx in advance
james

richard.york July 21st, 2004 07:12 AM

Hmmmm... interesting challenge.

I dunno much about Perl.

At first glance one possibility is to store the necessary variables in a PHP session variable, then open and parse the PHP session data file with Perl... but that leaves the question of how to pass the session id to Perl. I would imagine that using request variables like GET, POST or COOKIE, that the PHP session id would be available that way, whereas you could open the sesison data file from Perl.

Then that leaves the question of how to execute the PERL script.

Well, one approach would be to use a simple redirect using the location header.. being sure to include that session id... then whaalah.. you have access to the PHP session data from within Perl.

It sounds like a feasible solution to me.

Does that make sense??

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::

richard.york July 21st, 2004 07:24 AM

Ok.. here's a slightly more detailed answer...

Once your request variables are passed to PHP.. let's say via the POST method. Here is a very simple demonstration of what I was talking about.

<?php
    // This php script is the target of your form.

    // Begin the session
    session_start();

    // Transfer the POST variables to the SESSION variable, where they are stored in a session file.
    $_SESSION = $_POST;

    // Redirect to the Perl script, pass the session id via the GET method.
    location('header: perl_file.cgi?sid='.session_id());
?>

The location of the session data file varies depending on your OS. On Windows its commonly located at C:\PHP\sessiondata, on Linux its located at /tmp. You have to make sure you have the proper read permissions set to access the data.

The session file is named:
sess_232c53fe64de1a5e30df280db6095006

Where that long string of random numbers and letters is the session id.

Then the inners of the session data file looks like this:
user_id|s:2:"58";user_name|s:5:"richy";email|s:22: "me@mydomain.net";password|s:8:"mypassword";contro l|s:5:"index";

Each portion of this relates to a SESSION variable.

I dunno the required Perl.. but it shouldn't be too complicated to parse.




Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::

joconnor July 21st, 2004 07:49 AM

thanx for your help rich but unfortunately since i only started learning PHP about 2 weeks ago i have no idea what you mean about sessions, i've heard of them but thats about as far as my knowledge goes. if you know of any exmples on the net that i could look at then that would be great.
thanx again
james

richard.york July 21st, 2004 08:13 AM

I assume you have some background in programming??

The first place to start is the PHP manual.
http://www.php.net/session

That's the technical, and very dry explaination of each session function.

Sessions are a way to store and persist data between client connections. Its not a permenent storage receptical, like say a database is. Its a way to store data that's used while the visitor is using the website, once the user closes their browser, the session ends and at some point after that garbage collection happens, which is PHP's process of deleting outdated session files. Therefore, sessions are temporary.

In PHP every session is initiated with a call to session_start() at the very beginning of the PHP script. This function does the following:

1. It issues a cookie in the HTTP response headers to the client's browser. The cookie contains the session id, which associates the client with the session data that's stored on the server.
2. It creates a plain text file on the server, where session data is to be stored. (See previous post).
3. If a session already exists, it opens that session data file and populates session variables with the data contained in the plain text file. This data is contained in the $_SESSION superglobal variable.

Here is an explaination of special variables like $_SESSION..
http://www.php.net/manual/en/languag...predefined.php

$_SESSION is an array, therefore you use array syntax to create variables and to access them.

Here is a very simple demonstration of a session in action.

Code:

<?php

    // session.php
    // Any PHP script that requires access to session data
    // must make a call to session_start().
    session_start();

    $_SESSION['foo'] = 'This data persists between connections.';

    echo "<a href='session_2.php?sid='".session_id()."'>Go to the next page.</a>\n";

?>

Then the second file...

Code:

<?php
    // session_2.php
    // start the session
    session_start();

    // This variable still exists!
    echo $_SESSION['foo'];
?>

Finally because session_start() alters the outgoing HTTP headers, it may not have output before it.


Code:

This causes an error
<?php

    echo "This also causes an error";

    session_start();

?>

You can also Google for more examples.
http://www.google.com/search?q=PHP+session+tutorial

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::


All times are GMT -4. The time now is 11:30 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.