Jump to content


Photo

Help with variables


  • Please log in to reply
6 replies to this topic

#1 melvnatic

melvnatic
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 April 2006 - 12:11 AM

How do you make it so that a variable can be accessed from two different scripts?

Let's say you set a variable in script #1, and you want to access that variable in script #2 in the same directory as script #1. How do you do that?

Also, how do you use includes? What do you put in include(???). Can you give me an example?

Thanks!

#2 freshrod

freshrod
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 April 2006 - 05:44 AM

Greetings Melvnatic

Ok...so lets say you have a function you want to use a lot. You create a php file with that function.

<?php
function bold($string) {
print "<b>" . $string . "</b>";
}
?>

Save that as bold.inc or just bold.php.

Then you access it in your script by calling it with

include "bold.inc";
or
include (bold.php);

Then you can use the function like:
bold ("This is some bold text right here!");

OR

If you wanted to use it as/with a variable:

$boldPrint = "This is some bold text right here!";
bold ($boldPrint);

I hope this helps.
Nothing is foolproof to a talented fool.

#3 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 21 April 2006 - 06:24 AM

[!--quoteo(post=367050:date=Apr 20 2006, 05:11 PM:name=melvnatic)--][div class=\'quotetop\']QUOTE(melvnatic @ Apr 20 2006, 05:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
How do you make it so that a variable can be accessed from two different scripts?

Let's say you set a variable in script #1, and you want to access that variable in script #2 in the same directory as script #1. How do you do that?

[/quote]

You have a variety of ways to persist variables. They are in no particular order:

-pass as either url params or form variables
-store them in a cookie
-store them session variables
-write them to a database, read them out
-write to a file/read out
-write to berkely db/ read out
--- etc other IO options
-Use the shared memory extension to write to a shared memory variables using shmop functions


With databases, there's obviously a whole array of options there.


#4 melvnatic

melvnatic
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 April 2006 - 01:29 PM

Thanks!

How do I use session variables? Do they delete themselves when the visitor exits the page?

#5 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 21 April 2006 - 01:38 PM

$_SESSION['variable_name'] = "This is my variable";

If you have performed all the tasks you need to, you can use session_destroy(); to kill the session, or you can delete individual session variables by using unset($_SESSION[variable_name]);


If you are going to use sessions, you need to have session_start(); AT THE TOP of your document, before anything else.

so your document would start something like;

<?php session_start();
// some php stuff
?>
<HTML>
<HEAD>

etc etc.
wisewood: proven fact, I am both wise, and wooden.

#6 melvnatic

melvnatic
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 April 2006 - 08:57 PM

1 last thing...how do I kill a session variable on exit?

Oh, and so if I store something under
$_SESSION['variable_name']

I can call it up with
$_SESSION['variable_name']
in another script?

Thank you for your patience:)

#7 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 21 April 2006 - 10:44 PM

You seem to be clear on the idea that php has page scope, and that a script creates variables and destroys them when the script is done executing. Thus there is a need for persistence/session. This isn't unique to PHP at all -- it's the nature of web development due to the design of HTTP.

To demystify PHP sessions, they are basically files stored on the server. When you create a session variable by assigning a value to it, that value is stored in a session file on the server. Once a session variable is created in Script A, script B can use it, so long as you session_start().

The typical way this is achieved is that php assigns the browser/user a unique ID number called the session ID. This is what ties someone to a session. In most cases this ID is set as a cookie, which is why it appears that magically these otherwise invisible variables are persisting. The advantage over cookies alone is that the values don't have to cross the internet --- only the session ID is needed.

Session variables are like any other variable in tnat you can change them or unset them. So your example above is exactly right.

To remove a sessions variable called 'foo' --

unset($_SESSION['foo']);

This example assumes that register globals is off, which it should be these days ;)

Read the session portion of the php manual for more info.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users