PHP Variables.

  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1240
  • Loc: UK
  • Status: Offline

Post August 2nd, 2005, 11:01 am

I'm in an odd situation. I have a seperate php script included into the main INDEX php file.

This has a list of variables with values assigned to them. When i submit a form a variable will need to be made to that value thats been posted. So then when it forwards to another website it forwards to the variable thats identical to.

e.g)

variables.php
Code: [ Download ] [ Select ]
<?
$var1 = "google.com";
$var2 = "yahoo.com":
?>
  1. <?
  2. $var1 = "google.com";
  3. $var2 = "yahoo.com":
  4. ?>



index.php
Code: [ Download ] [ Select ]
include "variables.php";
if(isset($_POST['submit'])) {


$name = $_POST['name'];
//lets say $name is "var1"


$server = "$".$name;

header("Location: $server");
exit();

}
  1. include "variables.php";
  2. if(isset($_POST['submit'])) {
  3. $name = $_POST['name'];
  4. //lets say $name is "var1"
  5. $server = "$".$name;
  6. header("Location: $server");
  7. exit();
  8. }



Instead of the script forwarding to 'google.com' it tries to go to '$var1'.


What am i missing? Something im not doing right?
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 2nd, 2005, 11:01 am

  • placid psychosis
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jun 08, 2005
  • Posts: 284
  • Loc: Warsaw, IN
  • Status: Offline

Post August 2nd, 2005, 11:11 am

You're setting $server to the string $var1. You need to use:

PHP Code: [ Download ] [ Select ]
$server = $$name;


instead
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1240
  • Loc: UK
  • Status: Offline

Post August 2nd, 2005, 11:29 am

I get:

Quote:
Parse error: parse error, unexpected T_VARIABLE in /home/~~/public_html/~~/index.php on line 27


... with that.
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1240
  • Loc: UK
  • Status: Offline

Post August 2nd, 2005, 11:41 am

Sorry, but you were right. It worked.
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1240
  • Loc: UK
  • Status: Offline

Post August 2nd, 2005, 11:47 am

Sorry for doing three posts in a row.

What if the situation was that i need to make a variable like:


Code: [ Download ] [ Select ]
$nameis_var1 = "google.com";
$nameis_var2 = "yahoo.com";
  1. $nameis_var1 = "google.com";
  2. $nameis_var2 = "yahoo.com";


and it was like:

Code: [ Download ] [ Select ]
$name = $_POST['name'];
//lets say $name is "var1"


$server = $nameis_$name;
  1. $name = $_POST['name'];
  2. //lets say $name is "var1"
  3. $server = $nameis_$name;


$server variable would not work at all. I get an error as said in third post in this topic.
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 219 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2010 Unmelted, LLC. Driven by phpBB © 2010 phpBB Group.