melvnatic Posted April 21, 2006 Share Posted April 21, 2006 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! Quote Link to comment Share on other sites More sharing options...
freshrod Posted April 21, 2006 Share Posted April 21, 2006 Greetings MelvnaticOk...so lets say you have a function you want to use a lot. You create a php file with that function.<?phpfunction 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";orinclude (bold.php);Then you can use the function like:bold ("This is some bold text right here!");ORIf you wanted to use it as/with a variable:$boldPrint = "This is some bold text right here!";bold ($boldPrint);I hope this helps. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 21, 2006 Share Posted April 21, 2006 [!--quoteo(post=367050:date=Apr 20 2006, 05:11 PM:name=melvnatic)--][div class=\'quotetop\']QUOTE(melvnatic @ Apr 20 2006, 05:11 PM) [snapback]367050[/snapback][/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 functionsWith databases, there's obviously a whole array of options there. Quote Link to comment Share on other sites More sharing options...
melvnatic Posted April 21, 2006 Author Share Posted April 21, 2006 Thanks!How do I use session variables? Do they delete themselves when the visitor exits the page? Quote Link to comment Share on other sites More sharing options...
wisewood Posted April 21, 2006 Share Posted April 21, 2006 $_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. Quote Link to comment Share on other sites More sharing options...
melvnatic Posted April 21, 2006 Author Share Posted April 21, 2006 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:) Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 21, 2006 Share Posted April 21, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.