Welcome to WebmasterWorld Guest from 54.227.1.130

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Including a PHP Function in a header

     

lindajames

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

10+ Year Member



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

vincevincevince

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

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



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() );

lindajames

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

10+ Year Member



i've tried that but it doesnt seem to work, when it redirects to that URL it leaves the value of KEY empty

brotherhood of LAN

11:55 am on Jun 28, 2003 (gmt 0)

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



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.

lindajames

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

10+ Year Member



how do you mean? im not really a php guru, im still learning so might need to be less technial ;-)

brotherhood of LAN

12:07 pm on Jun 28, 2003 (gmt 0)

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



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.

vincevincevince

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

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



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

jaski

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

10+ Year Member



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

Jaski

jatar_k

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

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



or an other option would be

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

vincevincevince

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

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



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 :-)

jatar_k

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

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



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.

daisho

4:35 am on Jul 2, 2003 (gmt 0)

10+ Year Member



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]

Sander

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

10+ Year Member



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?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month