Welcome to WebmasterWorld Guest from 107.20.104.110

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Including a PHP Function in a header

     
2:45 am on Jun 28, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Mar 10, 2003
posts:321
votes: 0


Hi,

I have a PHP Function Code, and when i put the following code in my script, the function is executed:

<?php
New();
?>

That works fine. However, i want the value of New(); to be in a header Location. Ive tried the following but it doesnt work:

header( "Location: scripts/index.php?KEY=New();" );

any suggestions would be appreciated.

Cheers
Linda

3:37 am on June 28, 2003 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


you cannot call functions within quoted strings in that way - instead concact the function result with the string:

header( "Location: scripts/index.php?KEY=".New() );

11:33 am on June 28, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Mar 10, 2003
posts:321
votes: 0


i've tried that but it doesnt seem to work, when it redirects to that URL it leaves the value of KEY empty
11:55 am on June 28, 2003 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


How about echoing the variable within the function?

i.e. when the function is executed the variable will be echoed wherever you call the function.

11:57 am on June 28, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Mar 10, 2003
posts:321
votes: 0


how do you mean? im not really a php guru, im still learning so might need to be less technial ;-)
12:07 pm on June 28, 2003 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


neither am i ;)

what i meant was, youre calling this function to get a variable, so I guess at some point in the function you have the variable available.

Put echo $key, or whatever the name of the variable is, inside the function - so when the function is called it will echo the variable.

Sound OK? :)

//added
How about calling the function before the header...and just echoing the variable inside your header.

12:14 pm on June 28, 2003 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


what kind of value does New() return?
if it's not valid for a URL then that could cause the problem

try doing:

header( "Location: scripts/index.php?KEY=".urlencode(New()) );

if that doesn't work - write the value of KEY in manually as a fixed test - and check that you can read that - maybe it's being passed, but the script the other end has a bug.

and as said above, try:
echo "Location: scripts/index.php?KEY=".urlencode(New());

that will help to isolate the cause of the problem - always the first step in bugquashing

1:43 pm on June 28, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 11, 2002
posts:388
votes: 0


What is New() function doing? That information can be helpful to give a sensible answer :)

Jaski

4:42 pm on June 28, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


or an other option would be

$newval = New();
header( "Location: scripts/index.php?KEY=$newval");

5:43 pm on June 28, 2003 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


only if New() returns a valid string for a url, jatar_k.

eg, if New() returns " Hello World & His Dog "

then putting it on the end of a url will
1) cut off because of the non quoted space, and
2) cut off at the & as it denotes a new argument

that's why urlencode() would be nice to use :-)

6:02 pm on June 28, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


true but if you are using a function of your own design then the data should be validated before returning it, otherwise the function really isn't doing a very good job, is it.
4:35 am on July 2, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 31, 2003
posts:457
votes: 0


Hey lindajames,

From your first post it looks like New() is doing it's output directly ie.

function New() {
.
.
.
print $keyvalue;
.
.
.
}

In this case that is why you are having a problem. You want to change the function to not print/echo the keyvalue. Instead make the last line of the function read:

return $keyvalue;

Or what every value you want to return. Ensure that this is the last line of the function since the function will exit at that point and any lines after the return will never be executed.

For more information about return see this PHP Manual Page [php.net]

9:23 am on July 2, 2003 (gmt 0)

New User

10+ Year Member

joined:July 1, 2003
posts:10
votes: 0


Instead of using print, which will just put some text to your headers at a random spot, use return, ie:

function New() {
//blah blah
$value=$funl0rd;
return $value;
}

Besides, I don't know if New is a valid name for a function, isn't that a PHP reserved keyword?