Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

Variables

Recommended Posts

Are variables supposed to open ONLY on the page that the data is assisgned to a variable? I have a page that with a piece of data that has a variable assigned to it. I can use that variable anywhere on that page and the data from it will come up. On the next page I tried to use the echo feature to show that data like I would on the first page and that did not work. The page would not come up and showed an error. So, I don't know if that is the way it is or if I'm doing something wrong.

Share this post


Link to post
Share on other sites
[quote]Are variables supposed to open ONLY on the page that the data is assisgned to a variable?[/quote]

Yes.  PHP does not run constantly like a C or C++ program.  It executes the page, then it stops, which kills all variables.  You can have some variables persist by putting them into the session, or using cookies.

The exception to this is if you are writing a program for php-gtk...but then it's closer to writing a C program than a web app.

Share this post


Link to post
Share on other sites
Just as I thought. Thanks, but I have no idea how to make sessions or putting data in cookies.............

Share this post


Link to post
Share on other sites
[quote]Thanks, but I have no idea how to make sessions or putting data in cookies[/quote]

Refer to the manual for sessions...http://www.php.net/sessions

To keep data in a cookie, write it on small strips of paper and mix it into the dough.  You may also want to lower the oven temp to keep the paper from burning.  (http://www.php.net/setcookie)

Share this post


Link to post
Share on other sites
example of session:

page1.php
[code]
<?php
  session_start(); //start the session
  $_SESSION['foo'] = 'bar'; //make a session variable
  header('Location: page2.php'); //redirect to new script
?>
[/code]

page2.php
[code]
<?php
  session_start();
  echo $_SESSION['foo']; //output: bar
?>
[/code]

Share this post


Link to post
Share on other sites
Am I supposed to understand this some how? Lol. For a n00bie this doesn't make much sense. I appreaciate you trying hopfully I'll get better at this some day so I can start helping other people out and know what I'm talking about.  :)

Share this post


Link to post
Share on other sites
when you have a variable in a script that displays info from a variable to the user, and then the user goes to another page, you have to do certain things in order to get the same data to show on the new page. it doesn't automatically show up anywhere. you have to "carry it over" to the new page.  there are lots of different ways of doing this. It really depends on your needs, as to what exactly is the best way to go about doing this.  You can make a form for the user to fill out, and post the information to a new page. You can store your information in a database or a flatfile and have each page access the database or flatfile, retrieve the information and show it.  You can make a cookie - a variable stored on the user's computer, and each page would access that cookie.  You can make a session variable, which is mostly the same as a cookie, but stored on the server. Well, it's more complex than that, but we'll just leave it at that. 

The example shown be myself and others was a session variable. The idea is to tell your script that you want to start a session, which is basically a fancy way of telling the server that hey, you have some information that you want to carry over from one page to another, so pay attention.  then you declare the session variable, just like you would any other variable, except that it's stored in an array called $_SESSION. you would access the information just like you would access information from any other array. 

On each new page, you will always start with [b]session_start();[/b] that tells the server that you have information out there somewhere and you want to be able to get to it.  Then you would use the session variable just like a normal variable. 

you can try this out for yourself by making 2 .php files just like in the example i showed you.  make 2 files. one called page1.php and one called page2.php. copy/paste the contents of each one, as shown above. upload it to your server and goto page1.php in your browser. it will make the session variable, redirect you to page2.php and echo it out on the screen. 

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.