Share a variable between two php files on two different serv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Share a variable between two php files on two different serv

Post by Proqrammer »

Is there anyway to share a variable lets say an array between two php pages on two different servers?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Can ducks duck? ;)
The answer strongly depends on the actual problem.
Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Post by Proqrammer »

Thanks for your reply Volka,

Let's say I have a php page named "page1.php" hosted on "www.somesite1.com/page1.php"
In this php page I have declared a string variable such as $string1="foo";

Now there is another php page named "page2.php" hosted on another server at "www.somesite2.com/page2.php"
Now is there any built in function or method to have the value of string1 in page2.php ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Is there a client/browser "between" those two servers, i.e. the browsers request a document from server1 and then from server2 (probably because of something in the doc from server1, a link e.g.)?
Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Post by Proqrammer »

Nope.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Then who or what is when and how invoking the scripts?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Insert information into database and remotely connect?

Insert information into text file and read from different server?

Secondary server calling on a script directly for results in say XML/tab separated format?

This sort of thing is easily possible, but what you should do is identify whether or not your application is designed optimally because cross-server data exchanges can be costly for script processing time if the application is heavily reliant on this data.

If you feel there is no other way to have the system setup, whatever the system may be, then look into implementing one of the above ways. I'd pick based around what's best for my needs and what's optimal for security and loading times.

By the way, how often is this data updated? Do you want this exchange happening on a schedule in the background or do you want it happening at script run-time when a user is on the site? Can you give an example as to what the data is?

The more information you provide in your problem scenarios the much better it is for the people helping you find a suitable solution, as volka hinted at. ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

m3mn0n wrote:as volka hinted at. ;)
but only tight-lipped ;)
Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Post by Proqrammer »

The thing is I don't know much about it either, I wanna get employed somewhere and he asked me to solve this issue probably to let me know that I don't know anything about php!

I guess the best way around would be to include a sub in one of the servers is possible.
Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Post by Proqrammer »

I mean, is there any internal way to do it, rather than using a database, or any kind of files?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, sorry, there's no such magic built-in ;)
If the task really is that unspecific you should ask for clarification. m3mn0n gave you a good starter what might be possible.
Proqrammer
Forum Newbie
Posts: 10
Joined: Mon Nov 13, 2006 5:37 am

Post by Proqrammer »

Someone has told me I should use an include file with serialized data! does that make sense?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Proqrammer wrote:Someone has told me I should use an include file with serialized data! does that make sense?
Yes, but then again not really for just a single value. If it's a scalar value (i.e. a number or a string) then just dump out the value. If it's an array you could of course dump out the serialized string and unserialize() it from the other server.

Look at file_get_contents() but you'll need allow_url_fopen in php.ini for it to work remotely.

http://www.php.net/serialize
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Do some tests and see if that's suitable.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can always pass them as get variables or even post variables. cURL might be able to help also.
Post Reply