Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Defining Site-wide Variables/constants


I'm relatively new to PHP, and have just converted a site from ASP to
PHP. There is one thing I haven't managed to do, though.

When the site was using ASP, I had one file (called variables.asp),
where I defined various variables that are used throughout the site. I
could then access all of these in any other file by simply including
that file (using #include), then referencing the variables.

I have been unable to do the same thing in PHP. I have been reading the
PHP manual and searching Google, and it seems that perhaps it would be
better done with constants (the values don't ever need to be changed by
the script, just printed to the screen), but I am still unable to get it
to work.

Is this even possible, and if so, what am I doing wrong?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Site-wide Search Script
I need a way for visitors to search my entire site with the standard search box and search button. Can someone direct me to a simple script (free would be nice!) for this?

Defining Variables In A Seperate File
I am missing a trick here on how use variables defined in seperate file.

In script 1, I have proved to myself my class is working OK because I can
pass in the values and the echo statement outputs the value of localhost.

<?php
// Script 1
// This script works and outputs the name of localhost
require_once("class.MySQL.php");
$db = new MySQL("localhost", "user", "password", "database");
echo "Name of host: ".$db->host;
?>

However if I assign these values in a seperate file called configuration.php
and put the $variable names in the instantiation statement, it doesn't seem
to work. Can't understand why it doesn't work because I reckon it should
still be passing in the values of $localhost, $user, $password and $database
which is exactly as script 1.

<?php
/* Script 2
This script does not seem assign a value to $localhost
Here is the contents of config.php without the open/close PHP tags.
$host = "localhost";
$user = "user";
$password = "password";
$database = "database"; */

require_once("configuration.php");
require_once("class.MySQL.php");
$db = new MySQL($localhost, $user, $password, $database);
echo "Name of host: ".$db->host;
?>

Constants Or Variables
I am translating an existing script into another human language. I am
putting all human language stuff in one file.

At the moment I use constants in the following way:

<?php
define("LNG_Changes_saved_OK", "Changes saved OK");
define("LNG_Delete_Are_you_sure", "Delete: Are you sure???");
?>

It is not too late yet to change to variables (array of strings) like this:

<?php
$lng["Changes_saved_OK"]="Changes saved OK";
$lng["Delete_Are_you_sure"]="Delete: Are you sure???";
?>

What is better and why? Are there alternative ways of doing this and why
would they be better? Please consider in your answer things like server
load, memory usage and speed etc.

Constants Vs Variables...
Recently I decided to make a login script for my site using PHP and MySQL, after some reading/talking I came to the conclusion that constants are more secure.  It is going pretty good, there have been some rough spots, mostly because of the lack of variables. Code:

Define()ing Constants Vs Global Variables
I'd like to open a discussion regarding define()ing constants for use throughout a script, or simply declaring global variables. I'd like to know who uses what and why, and what you think the advantages / disadvantages of either are. Example PHP Code:

Server Wide Scripts In A Linux Environment
I have developed a content management system and every time I want to
create some short of server wide repository of the classes I am using.

So when I update a bug I don't have to go and edit the code or upload
the new file to every virtual site I have on the server.

Do you have any ideas of what is the best way to do it ?
I am on a Linux OS by the way.

Defining Large Array?
I tried to create an array with 1000 cells, keys 0 thru 999 using

$myarr = array (1000);

But this leads to an array of 1 cell with value 1000;
Now I have a workable solution

for ($i=0 ; $i < 1000; $i++) $myarr [$i] = 0;

And in the PHP manual online I found someones' solution using array_pad ();

But I would be surprised if there isn't a simpler, standard way of defining
an array of a given size.
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?

Defining Vars Outside The Class
Using PHP version: 4.3.7
I try to define my db vars outside the class!
Why this will not work?
class mytest{

//***************** set db settings ***************************
var $HOST ;
var $USERNAME;
var $PASSWORD;
var $DBNAME;

function setdb( &$db_HOST, &$db_USERNAME, &$db_PASSWORD, &$db_DBNAME){

$this-> HOST =& $db_HOST;
$this-> USERNAME =& $db_USERNAME;
$this-> PASSWORD = & $db_PASSWORD;
$this-> DBNAME = & $db_DBNAME;

// OK this will print the vars
echo ("dbHost= $this->HOST<br>dbUsername= $this->USERNAME<br> dbPassw= $this->PASSWORD<br>dbName= $this->DBNAME<br>");}
}

//test example
........
$dbhost = "localhost"; // DB Host name
$dbusername = "username"; // DB User
$dbpass = ""; // DB User password
$dbname = "mydb"; // DB Name
$setdbconfig=new mytest();
$x= $setdbconfig->setdb(& $dbhost,& $dbusername,& $dbpass,& $dbname);
........
$SelectedDB = mysql_select_db( $this->DBNAME) or die ("<br>database error= ".mysql_error());
$connection = mysql_connect($this->HOST, $this->USERNAME, $this->PASSWORD) or die ("<br>".mysql_error());

Site Counter Session Variables
I have a quick question I have some code here to update my site count everytime somebody hits a page on my website - it works. I just don't know if this is the best way and if what I'm doing is right. Any feedback would be great. Is it a better idea to use a global variable counter and if so does anyone have any examples.

session_start();
if (session_is_registered('count')==false)
{
session_register('count');
count_site_hit();
}

Defining Multiple Values In An IF Statement.
I am writing a file upload script, but only want the file types to be jpg, gif or png. I want the script to basically say "If this file type is not a jpg, gif or png, then echo a rejection" Code:

Php Constants In Php.ini
I am trying to set user_agent = PHP_VERSION in php.ini. In my php.ini
the instructions say that you can use constants in the php.ini file.
The only constants I can get to work, though, are the error level
constants like E_ALL, E_ERROR, E_WARNING, etc., ...

Is anyone aware of some sort of trick to getting others to work?

(I was thinking it would be nice to have the user agent string used by
my Php scripts to use PHP_VERSION in the string.)

--

List Constants
A little embarrassing but: is there any easy way to echo (the name and
value of ) all defined constants?

Getting Apache Constants With PHP
I'm working on implementing a secure cookie protocol I've been reading
about, but I've run into a problem. I was hoping some smarter people
than myself here could help me. I need to access an apache constant
(set when using mod_ssl) through PHP, or Perl (but I'd have to get it
back into PHP). Does anyone know how to do this?

I was wondering if you could do something like set a PHP variable from
Apache. It looks like they are doing something like that
http://us3.php.net/configuration.changes when they are setting php
initiation values. That means they talk at least one way, I was hoping
there was a way to send more information. If anyone has any ideas it
would be greatly appreciated, I figure there should be a way to, but I
am not sure exactly how you'd do it.

Use Of Constants Or Session Vars
Does anyone see a problem with using defined constants as opposed to $_SESSION vars for holding data like a users id, name, email, etc.. Basically when a user logs in, I register their relevant info in session vars and use them throughout the app to be used in queires. so theres a lot of PHP Code:

Mcrypt And Predefined Constants
Has anyone here worked with mcrypt? I'm having trouble getting
predicable results.

Example if I use 3DES and MCRYPT_MODE_ECB I cannot encrypt "157058"
and then decrypt it and get the same thing. The 8 is obscured as
someother character.

Example if I use 3DES and MCRYPT_MODE_NFO I cannot encrypt
"poiuy789" and then decrypt it and get the same thing. The 789 is
obscured as other characters.

My key and connect string are both 16 character.

Any Ideas. I am trying to create a login that encrypts the id and PW
in a cookie so I can decryp and compare it to a record entry. The
client wants the PW in plain text in the DB.

Can Constants Be Used Inside Header()?
i have something similar to this:

<?php
header("Location: http://mysite.com/destination.php");
?>

i would like to replace "mysite.com/destination.php" with a constant.

for example, with a constant DESTINATION defined as
"mysite.com/destination.php".

Use Of Constants In An Interpreter Environment
what's the use of having constants in an interpreted language like
php, when there's no compile cycle?

Other than enforcing programmer discipline or affording a convinience
for the programmers, I don't see any.

Defining A Class/instantiating Object Inside A Method Of Another Class
I have a class. Inside the class I have a method that reads a text file.
In the text file I have a class name and a file name where that class is
defined. Now, I need to instantiate an object of that class, the problem
being that at the time I find out the class name and the file name, I'm
inside a method of another class. Well, let me just type up a little
example:

Converting A PHP Site To A Total Static HTML Site
What's the best way to convert a PHP site to a total static HTML site? You might wanna ask why I wanna do this. Reason: I would like to create an offline version (without server) for demo purposes. Looking at my current PHP site, I am having problem dealing with this issue.

Site Flow Chart/site Map Creator For PHP?
I'm working on a site created by someone else, containing a hundred
different pages or so in PHP, with connections (includes and form
actions) going all over the place.

Is there a utility out there that will create a visual site map of all
the connecting pages in a site?
I tried using Dreamweaver MX 2004's, but the best it can do is see
connections in HTML. It'd only go one layer deep and find only a few
connections for a total of about 6 pages out of the dozens.

Copy Image From Site To Site Using URL
I'm trying to copy an image/file from one website to a directory in another, using the URL for the source image/file. I'm not sure which PHP function to use. Does anyone have a suggestion?

Site To Site Content
I am looking for a PHP function that can display content from a
separate site preferably with error handling. This is in lieu of using
frames. Something similar to the ColdFusion tag cfhttp.

Same Named Variables.. Or Addressing Variables In A Loop
I'm just starting to program in PHP.. fairly new to programming in general... and I've run across a (probably simple) programming problem I hope someone can help me with. I'm sending variable names through an HTTP get that are generated through a foreach loop. The names are along the lines of name1=, begin1=, end1=, name2=, begin2=, end2=.. etc. The number is the number of the current cycle through the loop. My problem is addressing the variables on the next page... namely how do I evaluate those variables in a loop? I want the value of name1.. but obviously

for (i=1; i<3; i++)
{
echo "$name$i";
};

is not working. Is there an easy way to do this?

Passing Variables & Dummy Variables
Hi! thanks for all the previous help - I've made some progress & have dropdowns and a checkbox; however I have 2 probs which I can't figure out: grateful for any further help. THANKS!

1. Not sure how to code so that user can query from just 1 or all of the dropdowns (at the moment unless they select the correct answers from each dropdown - no results!) = defeats the whole purpose of searching. By selecting from menu options user selects from 2 diff tables.

[the page allows user to search catalogue by selecting year of release, artist, music type - results from 2 tables need to be selected & sent to mysql.

I just don't know how to code it so that user must make an initial selection to get any results, and whichever menu options they choose "add them together" if you like to make a complete query.... sorry my coding just isnt deep enough!

A friend suggested using a dummy variable but as I'm a bit out of my depth; I need to get some help!

The coding I've done so far;

Passing Variables And Session Variables
I'm currently working on a shopping cart script that works just fine with register_globals turned on but when turned off i get all kind of errors. The following page displays a detail of the book with the "Add to cart" link at the bottom. PHP Code:

Passing My Variables With Forms Variables
how to carry variables onto the next page along with the forms variables. I need to carry my own over too, but when I press submit on the form page only the forms variables carry over, not my own ?? I have two drop down menues on my form that provide two values "start & end" which I require to go with my own variables.I have tried "a href=" link which carries my variables but not the forms variables.

<?php
echo"<form method=get action=somepage.php?one=$one&two=$two&three=$three&four=$four>";?>
also tried
<a href=somepage.php?one=$one&two=$two&three=$three&four=$four&start=$start&end=$end

Persistant Variables - Application Variables
In the books I have read about PHP I haven't been able to find any descriptions of how to set variables that persist in the servers memory, in the way that Application variables persist in Cold Fusion.

In CF, if I set an application variable then any of the application pages that are run will have access to those variables. And you can put complex arrays of data in the Application Variables and then access these variables at any time, without having to reload those variables for each page when it loads. I expect you can do this is PHP, but I can't find any info about how to do this.

Variables That Change Session Variables
I'm currently writing a mulit-page form app that uses a session to
retain data from each form element in order for the user to jump
between pages, then the final data is passed to a calculation script.

However, I've noticed that if I assign a session variable to another
variable, which then performs a mathematical calculation, the session
variable changes.

i.e. $_SESSION["inflation"] = 4;

$temp_inf = $_SESSION["inflation"];
if ($_SESSION["inflation"] =="RPI") {
$inflation = $rpi;}
else {$inflation = ($temp_inf/100.0);}

now it equals = 0.04

How can I take a value from a session and perform operations on it
without altering the original session value.

Form Variables To Session Variables
I am writing a small script that consists of two pages:  page one displays a bunch of values from a DB (ie. First Name, Last Name, Phone Number).  On this page you will find a form that follows this format:

<input type="text" name="edit_firstname" size="20" maxlength="35" value='Bob'>
<input type="text" name="edit_lastname" size="20" maxlength="35" value='Smith'>
<input type="text" name="edit_phone" size="20" maxlength="35" value='5552222'>

So when you see the page there will already be a 'default' value in each line (which is information extracted from a DB).  This works perfectly and has been tested thoroughly.

On the second page I have the following code:

$editfirstname = $_POST['edit_firstname'];
$editlastname = $_POST['edit_lastname'];
$editphone = $_POST['edit_phone'];

What I am trying to do is this:  the default value for edit_firstname from the form is 'Bob' by default, but the user can change this in the input field on the first page.  This value, whether changed or unchanged, should then be stored as edit_firstname and by calling up the function $_POST[''] on the second page I want to transfer the value to the variable $editfirstname. 

However, it seems that $_POST['edit_firstname'] and the other fields do not return a value at all, hence $editfirstname = '' or $editfirstname = 'NULL'.  I have never tried to do things this way before. 

In-Site IM
Is there any way to do an in site Instant Message system? Like AIM....?

How To Set Up Second Site Under IIS With Php?
Has anyone set up second IIS site, do i need a second php installation or is there a way to specify the doc_root?

Php.ini Per Site
I use php.ini on my site to execute scripts for every page (auto_prepend_file)
and also set some spefic settings in it (buffering,auto session start;..)

I am starting on the development of a second site that requires its own php.ini


How can I do this on a windows system?

PHP First Site
How should it be named the first site in a website in PHP? Is there any
special rule?
index.php for example?
I would like to know how it functions that by giving the wed address
(www.jfdhjdfh.de for example) comes the first site.
Is it like HTML that the first site must be named index.html?

Site That Support WAP
Hi, all

With PHP is it possible to build a site that support WAP?
How to do it. What kind of technology needed to build it? Can anyone here tell me where I can find the resource?

Downloading A Site
In php, is there anyway to download the HTML contents of a given site and store it in a variable for parsing?

IE "download the msn.com HTML into $html_var and do this parsing code on it" ?

Help For The Referrer Site?
I am developing a multi-affiliate counter server, so I want to record the referrer site when user visit one of our members.
There are four entity: viewer,referrer_site,member_site,ours(server site).

when viewer visit member_site from referrer_site, the member_site send a request of recording this visit to ours.

I know $HTTP_REFERER, but I think it will be member_site becouse the request to ours is from our member_site.

How can I get the referrer_site's host name?

Bandwidth From A Site
Real quick does anyone know how to get bandwidth from a site with php? I would like to give the bandwidth instead of COUNT per hit. so this site usage would be bandwidth: 83.67 Megabytes for the month

Latter I am going to attempt php graphic chart with monthly bandwidth. But I havent found anything on getting bandwidth yet.

Get Code From Another Site
I would like to (lets just call it) borrow a piece of code from another
website. The website in question is published totally in Microsoft
Frontpage, some very ancient version with totally flat HTML. (Not my site,
not under my control. The owners have no interest in changing the site)

Is it possible in PHP to take a piece of code from this website and
reference it on my page, I was thinking of something along the lines of:

GREP from sitename | referencing this page | inside these markers

I've spoken with my web developers and they tell me I can use Iframe tags or
RSS feeds, both of which I do not believe will allow me to accomplish this.

How To Create Site Map?
how to create sitemap? the sitemap i'm having now, if i want to add or modify things, i have to do it in the code.

Get All Links From Whole Site
I want to write an application what to extract all links from a whole site, but not work:
PHP Code:

Site Generator
Has anyone got any scripts that will, after a person registers, it will automatically create a directory for them?? Example:

User Registers -> Confiration Page -> User may now go to
http://mydomain.com/registeredusername with a default index.hml file

Also a File Manager. Does anyone know how to do this or where i can find such a script?

See Who Is On My Site, Live.
Is there a way to see who is on my site live and if so, how hard would it be to setup a chat scrtip so I could click on their IP or whatever and start chatting?

Regexp For Site Url
I would need a code that checks if an url with filename is valid or not.

Here are a few examples:

http://www.example.com/file.zip (valid)
http://example.com/file.zip (valid)
http://www.example.com/files/file.zip (valid)
ftp://ftp.example.com/file.zip (valid)
ftp://example.com/file.zip (valid)
ftp://ftp.example.com/files/file.zip (valid)

http://www.example.com/ (not valid)
http://www.example.com (not valid)
www.example.com/file.zip (not valid)
ftp.example.com/ (not valid)
ftp.example.com (not valid)

If anybody knows a regexp for this please share with me!

Site Search
Can anybody tell me how to create a site search feature using PHP in a web site.

Site Last Visited
when people visit your site is there any way to know which site they came from?

Site Statistics
Does anyone know of a PHP application that can analyze server traffic similarly to the professional version of SuperStats (http://www.superstats.com/demo_professional.html)?
It doesn't have to be free, but good and extensive.

Banning On Site
Is their a way of checking a file full with ip and if an ip matches one on the list they get transfered to another site? this would really be of usefull to me.

Browse A Web-site
I want to use a PHP script to browse a web-site on which i do exactly the smae things time after time. The website uses cookies to log users on, here is my first problem.

I have been informed that if i simply get a script to logon the web-site will send the cookie to the PHP script and not my browser. Then the PHP script will ignore the cokie and the site will not regard em as being logged on. How can i go about saving this cookie?

Upon entering the web-site how can i serach the content of the site for a time? The time is always in the same place and i would like to be able to update the page every minute or so to check the time, as some actions have to be done at a certain time.


Copyright © 2005-08 www.BigResource.com, All rights reserved