/    Sign up×
Community /Pin to ProfileBookmark

Passing Variable to Function

What am I doing wrong here? Further up in the script I am setting rowid from a form (using get), and then I tried using just the ‘global $rowid;’ to bring it into the function. When that didn’t work I put $rowid as a variable inside the function login_get($rowid); and passed the rowid to it that way.

Now that half works…
For some reason, I can get the [i] echo “<tr><td>Username : $rowid</td><td><input name=’user’></td></tr>n” ;[/i] bit nearer the bottom to display the variable there, but it won’t display it in this bit [i] echo “<meta http-equiv=’refresh’ content=’20; url=?rowid=”.$rowid.”9′><a href=’?’>Wait … $rowid</a>” ;
exit;[/i]

Any ideas what i’m doing wrong?

[code=php]
function login_get($rowid)
{

if (isset($_POST[‘user’]) && isset($_POST[‘passwd’])) {
global $rowid;
setcookie(“protect_user”, $_POST[‘user’], time()+864000, ‘/’) ;
setcookie(“protect_passwd”, crypt($_POST[‘passwd’], ‘pc’), time()+864000, ‘/’) ;
global $rowid;
echo “<meta http-equiv=’refresh’ content=’20; url=?rowid=”.$rowid.”9′><a href=’?’>Wait … $rowid</a>” ;
exit;
}

require(“header.php”);
echo “<table width=’100%’><tr><td align=’center’><table>n” ;
echo “<form action=’?’ method=’post’>n” ;
echo “<tr><td>Username : $rowid</td><td><input name=’user’></td></tr>n” ;
echo “<tr><td>Password :</td><td><input type=’password’ name=’passwd’></td></tr>rnn” ;
echo “<tr><td>&nbsp;</td><td align=’center’><input type=’submit’ name=’Login’ value=’Login’></td></tr></form></table></td></tr></table>” ;
require(“footer.php”);
die();

}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@yunaOct 07.2004 — Take out the "globals $rowid" statements from the function. You don't need them if you're passing $rowid as a function argument.
Copy linkTweet thisAlerts:
@GavinPearceauthorOct 07.2004 — Why should that make any difference if I only have the content of the variable set once at the start of the script and never changed?

Ok so now I have...

[code=php]
function login_get($rowid)
{

if (isset($_POST['user']) && isset($_POST['passwd'])) {
setcookie("protect_user", $_POST['user'], time()+864000, '/') ;
setcookie("protect_passwd", crypt($_POST['passwd'], 'pc'), time()+864000, '/') ;
header("Location: ?rowid=$rowid");
exit;
}

require("header.php");
echo "<table width='100%'><tr><td align='center'><table>n" ;
echo "<form action='?' method='post'>n" ;
echo "<tr><td>Username : $rowid</td><td><input name='user'></td></tr>n" ;
echo "<tr><td>Password :</td><td><input type='password' name='passwd'></td></tr>rnn" ;
echo "<tr><td>&nbsp;</td><td align='center'><input type='submit' name='Login' value='Login'></td></tr></form></table></td></tr></table>" ;
require("footer.php");
die();

}
[/code]

which still echos the right thing on the Username: $rowid part but just doesn't echo anything for the $rowid in the [i]rowid=".$rowid."9'[/i] part.
×

Success!

Help @GavinPearce spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...