/    Sign up×
Community /Pin to ProfileBookmark

Not understanding sessions

Hi. I am trying to develop a auto-populated form based in a login script. I got the login authentication to work but when I redirect to new page with form I am unable to get the variables I need to query. I’m a PHP newbie, please help me understand what I’m doing wrong.

[CODE]<?php
session_start();
if(!session_is_registered(myusername)){
header(“location:main_login.php”);
}
//connect to server & db
// username and password sent from form
$myusername=$_SESSION[‘myusername’];
$mypassword=$_SESSION[‘mypassword’];

$sql=”SELECT * FROM $tbl_name WHERE username= $myusername “;
$result=mysql_query($sql);
$login_check = mysql_num_rows($result);
if ($login_check > 0){
extract(mysql_fetch_array($result));
$_SESSION[‘Company’] = $Company;
$_SESSION[‘FName’] = $FName;
$_SESSION[‘LName’] = $LName;
$_SESSION[‘Email’] = $Email;
}
else
{
echo(“Not Working!”);

}
?>
[CODE]
Thanks!:)

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@blue-eye-labsNov 13.2009 — <i>
</i>session_is_registered()


is deprecated and not present in PHP 6 so use

<i>
</i>array_key_exists()


instead since $_SESSION is just an array. You could also use

<i>
</i>isset()


http://uk2.php.net/session_is_registered

http://uk2.php.net/isset

http://uk2.php.net/array_key_exists
Copy linkTweet thisAlerts:
@StaceyBauthorNov 13.2009 — Thanks, I tried your suggestion but got errors.

"Warning: Wrong parameter count for array_key_exists() ..."

So if my variable isset() or session_is_registered passes, why can't I pull out the $myusername variable for the query?
Copy linkTweet thisAlerts:
@NogDogNov 13.2009 — string literals in SQL need to be quoted (just like they do in PHP):
[code=php]
$sql="SELECT * FROM $tbl_name WHERE username= '$myusername' ";
[/code]

I won't guarantee there aren't other problems, that's just the first thing that jumped out at me. You may want to check if the return value from mysql_query() is false, and if it is, error_log() or user_error() the query and the output from mysql_error() to find out what's wrong.
Copy linkTweet thisAlerts:
@StaceyBauthorNov 13.2009 — Thanks! Ok after further investigation, I've discovered that myusername is not registered in the session as I thought bc when I submit an echo, it returns nothing. My login page includes the following code:[CODE]session_register("myusername");[/CODE] before I redirect to my form page.

Any clues on passing the myusername variable?
Copy linkTweet thisAlerts:
@NogDogNov 13.2009 — Forget the session_register() stuff and just use the $_SESSION array.

[code=php]
<?php
session_start();
// do your login stuff, then...
$_SESSION['myusername'] = $valueFromLoginStuff;
[/code]

If you do a redirect via a PHP header() call, you may need to ensure that the session data is stored:
[code=php]
session_write_close();
header('Location: http://yoursite.com/landing_page.php');
exit;
[/code]
Copy linkTweet thisAlerts:
@StaceyBauthorNov 13.2009 — when I echo $_SESSION['myusername'] it is returning "Array"

Totally confused?
Copy linkTweet thisAlerts:
@StaceyBauthorNov 13.2009 — THANK YOU NOGDOG!!! I am now passing the variable!!! You've made my day!!!
Copy linkTweet thisAlerts:
@blue-eye-labsNov 14.2009 — Thanks, I tried your suggestion but got errors.

"Warning: Wrong parameter count for array_key_exists() ..."

So if my variable isset() or session_is_registered passes, why can't I pull out the $myusername variable for the query?[/QUOTE]


I included links to the documentation for those functions, they aren't session specific.

E.g.
[CODE]
(bool) array_key_exists($key, $array);
(bool) isset($array["key"]);
[/CODE]
×

Success!

Help @StaceyB 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.19,
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,
)...