Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 10th, 2004, 10:10 PM
Registered User
Join Date: Jan 2004
Location: , , Greece.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can I declare variables visable from every script

Hello and thanx in advance!
I would like to declare a variable that would be in some way global!
Let me explain!I want a variable that I can give it a value when my first php script runs and to be able to access this variable in every other scrip of my application with the $ symbol.I know that this can be done when you submit this value in every script with
<INPUT TYPE='HIDDEN'> for example from scrip1.php to scrip2.php to script3.php etc.My main concern is that I want this value to retain it's value and when the script is finished!So when the script will run again I want my variable to have the value that was assign to it when the script runned for the first time.This is like I want a variable that lives in the server and retaines it's value.

I hope someone can show me the way! ;)
Old January 11th, 2004, 02:02 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

You need to use sessions.

Basic tutorial:


/* script1.php
 * session_start() does the following:
 * If a session_id already exists, open up the session file related to that id
 * and parse the data contained in that file into session variables, which are accessed 
 * in a script via the $_SESSION superglobal
 * create new HTTP header information in the form of a cookie and output that cookie 
 * to the user's browser, if the user has cookie's enabled this will be one form of
 * passing the session_id from one script to another.
 * Because this function alters the outgoing HTTP response headers it must appear at 
 * the very beggining of a script before any output (e.g. from echo or print) and 
 * the opening php delimiter '<?php' may not contain white space or html content before 
 * it as that is considered to be output.  However non-output producing code, like these 
 * comments are o.k.
 * Read the following for more information
 * http://www.php.net/session - all session related functions
 * http://www.php.net/session_start


// Create a session variable for persistence
// The $_SESSION array is a special superglobal variable available in any scope
// More on PHP predefined variables:
// http://www.php.net/manual/en/languag...predefined.php

$_SESSION["foo"] = "Hi I'm persistent!";
$_SESSION["counter"] = 1;

echo "This session has been accessed: {$_SESSION["counter"]} times.";

Now to create the persistence.
If users do not have cookies enabled you might want to hard code in the session_id and pass it to every page that uses session data (a good idea!). The session id is a randomly generated string of letters and numbers that PHP uses to tie session data to the current user.

One method is with url embedded arguments a.k.a. query strings a.k.a. the get method:
echo "<a href='some_session_page.php?sid=".session_id()."'> another session page</a>";

echo "<a href='some_session_page.php?".SID."'>another session page</a>";

'SID' is a special predefined constant that will contain the entire string needed for a URL embedded argument.

Using the post method:
echo "<input type='hidden' name='sid' value='".session_id()."'>";

You may use either 'sid' or 'PHPSESSID' as the name of the session.
You don't have to do anything special for cookies, cookies are always used by default, unless you explicitly turn them off in php.ini.

Now you can access the variables we set above on another page:


// some_session_page.php
// Always make a call to session_start() on a page that uses session data 


// If this variable exists and its a number, do the following

if (isset($_SESSION["counter"]) && is_numeric($_SESSION["counter"]))
    echo "This session has been accessed: {$_SESSION["counter"]} times.<br />";

// If this variable exists and it isn't an empty value (false, null, empty string, zero, string zero)

if (isset($_SESSION["foo"]) && !empty($_SESSION["foo"]))
    echo $_SESSION["foo"]."<br />";

See there, pretty easy stuff!

: )

Smiling Souls

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to declare variables rehana Beginning PHP 11 April 30th, 2013 04:59 AM
send variables from a PHP script..... ethantinder Beginning PHP 2 June 23rd, 2008 02:45 PM
Passing & retrieving variables tto another script topshed Classic ASP Basics 5 October 11th, 2007 05:07 AM
HOW DECLARE VARIABLES IN PHP jhan316 Beginning PHP 1 July 25th, 2006 07:16 PM
how to declare date variables rehana Beginning PHP 1 December 15th, 2003 05:56 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.