homepage Welcome to WebmasterWorld Guest from 54.242.231.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Including a PHP Function in a header
lindajames




msg:1293464
 2:45 am on Jun 28, 2003 (gmt 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

 

vincevincevince




msg:1293465
 3:37 am on Jun 28, 2003 (gmt 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() );

lindajames




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

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




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

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




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

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

brotherhood of LAN




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

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




msg:1293470
 12:14 pm on Jun 28, 2003 (gmt 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

jaski




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

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

Jaski

jatar_k




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

or an other option would be

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

vincevincevince




msg:1293473
 5:43 pm on Jun 28, 2003 (gmt 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 :-)

jatar_k




msg:1293474
 6:02 pm on Jun 28, 2003 (gmt 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.

daisho




msg:1293475
 4:35 am on Jul 2, 2003 (gmt 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]

Sander




msg:1293476
 9:23 am on Jul 2, 2003 (gmt 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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved