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




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:




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
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.

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:

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?

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.

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. 

Building Variables From Variables
I am trying to build a variable, that will have a amount of that
variable, can someone describe/explain in detail the best method to do
this?

Variables
I have a variable holding text which I want the text to have a $ in front of it to convert it to a variable.

Php Variables
I'm trying to assign a new value to a variable passed from an html form to a php3 script. I'm using an if statement to evaluate variable 3 to see if it is equal to a specific string, if it is I'm reassigning variable 2 to be variable2+variable 3.

if ($question3!="select days")
{
$question2=$question2+$question3;
}

This is the statement. It keeps giving me a parse error and I can't seem to find the right documentation to show me how to combine two string variables.

Variables
If i created an array that holds fields of a table, how could I call one of the fields ie $array[field] if i need the field part to be a variable?

What Does @ Do In Variables?
i've noticed that many scripts contain @ before a funciton is called as in the following:
$host = @gethostbyaddr($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]);

so what is the purpose of the @ ?

Variables In URL
I have tried the following to pass a variable queried from a database to another page through the URL (id is the database record):

$idnum = $row->id;
print "<a href="page.php?$idnum>";
print "Click Here";
print "</a>"

and then to print the results on the linked page:

print $idnum;

but it didn't work. What's the exact code to retrieve and print the variable $idnum?

Also, can I use $idnum as a variable in a database query string such as

$result = mysql_query( "select id from articles where id is like $idnum" );

JS Variables In Php
i've got a piece of javascript code which prints out the city + country of a visitor to a website. I want to print both, city & country to a file. JS doesn't have file I/O.

Would it be possible to somehow import the variables to php? here's the JS code:

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity">
</script>
<script language="Javascript">
document.write("<p>Welcome to visitors from "+sGeobytesCity+", "+sGeobytesCountry);
</script>

Variables Within Variables
The actual app I am building allows users to create an email which is then mailed out members from a db of email addresses. Although I am having a big problem reading a variable into the mail after the mail has been created. In a very simplified state all I am trying to do is this (the order of events has to be this way unfortunately):

$content = "This is an email and the variable $name needs to be viewed at this point";
$name = "buttie";
echo $content;

When $content is echoed the variable $name does not get read. What is the best way to let $content access the variable $name, using this order of events.

Variables By URL
Hi have a simple php script that gets the website URL from a form... puts it on a variable and calculates Link Popularity. How can I get this to work passing the site URL by the address instead of the form.

[php]
<?php
if(isset($_GET['getlp'])){
// Edit this to your website url:
$myWebSite = ($_POST['url']);
[/php]

I go to
http://localhost/novo2/material/linkpop.php
On the form I introduce my site and the address becomes
http://localhost/novo2/material/linkpop.php?getlp=1
How can I avoid introducing the address and make the script assume that the address it needs its the address where it's being host?

PHP Variables Put Into A PDF
I have been searching for at least 5 hours all over the web and can't find a single thing to help me with this problem... I have a form created in Adobe Designer 7.0 (editable form) and I want it to receive variables from PHP and display the filled in form to users so that they can print it off for their own records. The form HAS TO BE laid out in the format I created in Designer 7.0. Any tips, suggestions, refering stuff?

Variables Through URL
I am trying to pass a variable through a URL eg. index.php?page=Features. Sadly, I used to do this in the Global off environment of PHP Nuke, and now have to cope with Global on. I have tried PHP Code:

Get URL Variables
how to get a variable passed in the URL, like this: <a href="issue.php?ticket=2">2</a>

On the issue page how do I get the variable 'ticket'? There seems to be no HTTP_URL_VARS or something of that nature and nothing in the PHP manual that I could find.

Variables?
I simply want to print some text that has 2 variables in it. Here is the code i have now.
PHP Code:

Is There A Way To Do This With Variables?
I have for example $f1, $f2, $f3, $f4... $f22.

i want a

for ($i = 1; $i <= 22; $i++)
{
if ($f$i == 1) { } // The variable should turn into $f1, $f2, etc
}

Php URL And Variables
It's probably very easy, but I cannot find my mistake:

page1.php

<?php
echo "<a href=page2.php?vartrans=5342>Clic</a>";
?>

page2.php

<?php
echo "$vartrans";
?>

When I click on the link on page 1, page 2 appears on the screen but with
the following error message:

Notice: Undefined variable: vartrans in c:program
fileseasyphp1-7wwwpage2.php on line 2

What's my mistake?

I use php 4.3.3.

Variables
i have many pages (page1.php .. page100.php) and each of them include
some fixed subpages (top.php, bottom.php).
each subpage (top.php, bottom.php) uses a variable ("theme") defined
in its "parent" (one of the pageX.php).
this variable is also used (not only defined) in the parent page
itself. i would like to have the variable "theme" defined in a single
external file, instead of defyning it for every page. is it possible?

sorry for the probably stupid question, im new to php.

Variables
Let me explain what i want with an example.

I have 3 files :-
File-A
File-B
File-C

I am using a variable called $tdir in File-C.
Its default value is stored in File-B.

Now, there is a Form in File-A where i can input a certain value and when i click "Submit", the value will be stored as $tdir in File-B.
From then, this new value will be used in File-A.

Get Variables
I want to transfer a variable to the next page. Here is the code:
First Page:
$page="1";
<a href="fav.php?page=$page">I like it!</a>
Second Page, (retrieves the variable)
$page = $_GET['$page'];

Sum Of Two Variables.
Im creating a small stats counter (god knows why after all the trouble it ahs caused me!) for my site, and getting all of the relevant data from the server log files. Getting the data isnt a problem, and slicing it up is fine but..

Ive created a rule that is trying to emulate a google analytics feature (the feature will count a visitor twice if they return after a certain amount of minutes even if its the same ip), but the code doesnt work. There are loads of different ip's in the log so I just cant understand why the code is interpreting just one ip. Code:

Getting All Variables
anybody know a way of getting all variables? the php parser engine must remember what each variable is... i just want to be able to make a debug type function which retrieves all of them....

If Problem With Some Variables Containing The Same Value!?
Hi all,

I feel a bit dizzy. I have been sitting in front of this programm for 12 hours now or so and now I only wanted to add a little function which checks if the user is allowed to see edit and delete links.
pretty easy hmm?

ok here is the code:

Code:
function showsubmenu($userid, $dbName, $libid){
$lur="SELECT `ur_edit`,`ur_remove` FROM $dbName WHERE `user_id` = $userid";
$lurquery=mysql_query($lur);
$lurresult=mysql_fetch_array($lurquery);
$ur_edit=$lurresult[ur_edit];
$ur_remove=$lurresult[ur_remove];

// just for checking if the rights come correctly from the db
echo "uredit= $ur_edit<br>ur_remove=$ur_remove<br>";

// edit link
if($ur_edit == 1){
echo "<a href="edit.php?action=edit&libid=$libid class=head>edit</a> | ";
}

// delete link
if($ur_remove == 1){
echo "<a href="admin.php?action=delete&libid=$libid class=head>delete</a> | ";
}

// back link
echo "<a href="javascript:history.back();" class=head>back</a>";
}

The idea is pretty easy. I got one additional table where the system stores rights for every action.
The name for editing items is "ur_edit".
0 is not allowed
1 is allowed

so as Admin, the system returns 1 for $ur_edit and $ur_remove. the query is correct!

The problem:

Only links displayed are delete and back. if I remove the edit lines, delete also vanishes.

If I remove the delete lines, only the back link will be displayed.

it should be:

edit | delete | back

but if you use the code above you only get:

delete | back

The syntax really seems ok to me and $ur_edit and $ur_remove are both 1.

What am I doing wrong here??

Truncating Variables
I would like to truncate a variable to 0 decimals.. no rounding just choping the decimal places right off.

Dynamic Variables In Php?
Hi,
I have a mySQL table that contains two columns
column one is 'feature' and column two is 'feature_value' - some of the data is for example

'hair_color' - 'blue'
'hair_color' - 'blonde'
'hair_color' - 'black'
'hair_color' - 'gray'

'eye_color' - 'blue'
'eye_color' - 'hazel'
'eye_color' - 'brown'

Yeah I know... definitely NOT third normal form. OK here's where it gets even better

On one web page, I build select controls from this data. The name of the control would be the value of the feature column. So from the data above I would have a select control whose name would be name="hair_color" and another select name="eye_color"

NO problems so far.... ok.
Now after the user selects from the selects (get it, sorry).... i am processing the data.

Although I use javascript for some client side validation... I also want to validate in php. (ok, just because I want to ok)

NOW.. the web page form will post these two variables $hair_color' and '$eye_color' as described above. But I don't know ahead of time what these variables are because they're based on data from a table.......

I go the table described above and select distinct on the feature column - I will return two rows.... see below

while($row = mysql_fetch_row($result_set)) {
$feature =trim($row[0]);
}

the value of feature in the first row is 'hair_color' and in the second row 'eye_color'--


What I'd like to do.... is to take the values in the $feature variable and create a literal variable that I can test for that was posted from the form........

so for example in the first row where $feature = 'hair_color' - I want to create a $hair_color variable...... because this is a variable of the select control from the form. Then for the second row where $feature = 'eye_color' - I want to create an $eye_color variable because again... this is the variable of a select control on the form.

Variables W/ Space Gets Cut Off On GET
I'm not sure why my text spaces aren't getting changed to %20. When I click a link w/ variables in it, the next page only gets the first word contained in the variable.

Any idea why this is happening? I don't remember having to do anything when passing multiple worded values across pages.

Invisible Variables - How To
I have a tagged iframe and two variables ($loginuser and $password) in a script that I am trying to write. I want to include them as part of the URL href to a target program for rendering in the iframe without using javascript. The following works:

Code:
$src="http://www.host.tld/prog.php?
loginuser=$loginuser&password=$password";
echo "<iframe id='myIframe' href=$src>No Iframes</iframe>";

but is undesireable. The variables can be read in the browser with View/Source. Same thing if I use target="myIframe".

The only way I have been able to do this without javascript is to use the php script to write a temporary file (middle.php) that includes a redirection with the variables as in:

Code:
$src="http://www.host.tld/prog.php?
loginuser=$loginuser&password=$password";
header(Location: $src);
exit;

and then call it from the main program as:

Code:
echo "<iframe id='myIframe' href='middle.php'>No Iframes</iframe>";

and finally deleteing the temporary file.

This works ok and there is nothing in the browser to show the value of the variables nor is there anything in the frame doucment source. I don't like writing temporary files if I don't have to, however, and I think this approach won't work for a target program (http://www.host.tld/prog.php) that is expecting post variables. Anyone have an idea about a better way to do this, given the constraints stated?

Passing Variables
here is some of my code
why wont it work it works when i replace $hello with $smith


<input type="text" name="hello" value="smith">
<?php
$db = mysql_connect("", "", "");
mysql_select_db("",$db);

$result = mysql_query("SELECT first,last FROM employees where last='$hello'",$db);

Cannot See Variables Within Function
I am quite blocked now. I cannot echo the variables inside the function.

Passing Variables
here's some code that's at the top of my page, a form later in the page posts the variables back to the same document and then updates the database. How do I make the page so that when the form is submitted it posts the info to the same page and then passes the same variables that were just put into the database to a page named view.php? or another way that would work was if there was a preview button next to the submit button that passed the variables, but the form action is set to PHP_SELF, so how would I make a button that would pass the form variables to view.php and not to itself?


Here's the code PHP Code:
<?php

Result Using Two Variables
Probably a real simple solution but I can't for the life of me work it out.

I have:

$pollitem_1
$pollitem_2
$pollitem_3
$pollitem_4

if I do a for loop for the amount of items, is it posiible to have a variable for the number after the _ ?

e.g....

Passing Variables
I am new to this and learning slowly, but I was hoping someone could give me some quick advice on how to create a variable on one page and have it passed to another to be read as an ID for a mySQL database.

I want to pass the variable "varRevID" from one page and have it read as "reviews_id" on the other.

My database is pulling from just the first record and I would liek to designate from the pointing page which record to pull from.

Passing Variables
Could somebody please give me a hint on the following problem.
1. I need to pass variables from one php file to another.
2. I need some few different forms and want to pass every variable to another (same) page.


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