/    Sign up×
Community /Pin to ProfileBookmark

"Notice: Undefined variable:" for session variable

After reading [URL=http://www.sitepoint.com/article/quick-php-tips]this[/URL] I decided to turn on error reporting full (just for sh1ts and giggles). I receive the following error
Notice: Undefined variable: myusername in /home/www/gramac4.freeserverhost.com/forum/login.php on line 9

this is the code in concern.

[code=php]echo “<p style=’text-align:right; margin-right: 30px’>n”;

if (isset($_COOKIE[‘cookname’]) && isset($_COOKIE[‘cookpass’])) {
$_SESSION[‘myusername’] = $_COOKIE[‘cookname’];
$_SESSION[‘mypassword’] = $_COOKIE[‘cookpass’];
session_register(“myusername”);
}[/code]

I though that $_SESSION[‘myusername’] = $_COOKIE[‘cookname’]; would define the variable.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@geomirMar 02.2006 — Hi ?

You are correct, it should define the variable.

In your code you use both $_SESSION['myusername'] and session_register("myusername")

$_SESSION['myusername'] = $_COOKIE['cookname'];

is equilivant to

$myusername=$_COOKIE['cookname'];

session_register("myusername");

so you do not have to use both.

An explanation for the warning might be that in case this is the first visit to the page and

cookies cookname, cookpass are not set then the expression $_SESSION['myusername'] = $_COOKIE['cookname'] is not reached and as a result $myusername remains undefined.

[code=php]
error_reporting(E_ALL);
session_start();
//session_destroy(); //destroy session for debug
//$_COOKIE['cookname']="sss"; //set var for debug
//$_COOKIE['cookpass']="ssddf"; //set var for debug
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {
$_SESSION['myusername'] = $_COOKIE['cookname'];
$_SESSION['mypassword'] = $_COOKIE['cookpass'];

}
echo $myusername;
[/code]


I wish I helped you. ?
Copy linkTweet thisAlerts:
@DoppleauthorMar 02.2006 — That's right because once you log in using the "remember me" option, the error is no longer displayed. Sorry I don't understand the part in the comments you've kindly put in.

Alternatively, would the folowing resolve the error?
[code=php]error_reporting(E_ALL);
session_start();

if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {
$_SESSION['myusername'] = $_COOKIE['cookname'];
$_SESSION['mypassword'] = $_COOKIE['cookpass'];

} else {
$myusername = '';
}
echo $myusername;[/code]
Copy linkTweet thisAlerts:
@geomirMar 02.2006 — Your modification will work just fine, great work!!!


? ? ?
Copy linkTweet thisAlerts:
@DoppleauthorMar 02.2006 — Ok that works in the sense that it get's shot of the error but then tehre's a problem that if you log in without using the cookies, it doesn't display the username (because it's been set to ''). Below is the whole log in code. how would i sort this? Also could you explain the comments in your earlier post please?
[code=php]<?php
echo "<p class='login'>n";

if (isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {
$_SESSION['myusername'] = $_COOKIE['cookname'];
$_SESSION['mypassword'] = $_COOKIE['cookpass'];

}
$myusername = stripslashes($myusername);
if (!session_is_registered("myusername")) {
echo "<table>n
<form action='checklogin.php' method='post' name='authentication'>n
<tr>n
<td><span class='inputlabel'>Username:</span></td><td><input class='input' type='text' size='25' name='myusername' /></td>n
</tr>n
<tr>n
<td><span class='inputlabel'>Password:</span></td><td><input class='input' type='password' size='25' name='mypassword' /></td>n
</tr>n
<tr>n
<td><span class='inputlabel'>Remember me?:</span></td><td><input type='checkbox' name='remember'>(requires cookies)</td>n
</tr>n
<tr>n
<td>&nbsp;</td><td><input class='input' type='submit' name='submit' value='Log in' /></td>n
</tr>n
</form>n
</table><br >n
<a href='register.php'>Register</a>";
} else {
$myusername = stripslashes($myusername);
echo "Welcome $myusername</p>n
<p style='text-align:right; margin-right: 30px'>n
<a href='userdetails.php'>My Details |</a>n
<a href='logout.php'>Log out |</a>n";
} ?>
<a href='userlist.php'>Userlist |</a>
</p>[/code]
Copy linkTweet thisAlerts:
@geomirMar 03.2006 — Is this the checklogin.php file?
[code=php]
<?php
session_start();

echo "<p class='login'>n";

if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {
$_SESSION['myusername'] = $_COOKIE['cookname'];
$_SESSION['mypassword'] = $_COOKIE['cookpass'];

}
else if(isset($_POST['myusername']))
{
$myusername = $_POST['myusername'];
$_SESSION['myusername']=$myusername;
}
else if(isset($_SESSION['myusername']))
{
$myusername = $_SESSION['myusername'];
}
else
{
$myusername = '';
}

$myusername = stripslashes($myusername);
if (!(isset($_SESSION['myusername']))) {
echo "<table>n
<form action='checklogin.php' method='post' name='authentication'>n
<tr>n
<td><span class='inputlabel'>Username:</span></td><td><input class='input' type='text' size='25' name='myusername' /></td>n
</tr>n
<tr>n
<td><span class='inputlabel'>Password:</span></td><td><input class='input' type='password' size='25' name='mypassword' /></td>n
</tr>n
<tr>n
<td><span class='inputlabel'>Remember me?:</span></td><td><input type='checkbox' name='remember'>(requires cookies)</td>n
</tr>n
<tr>n
<td>&nbsp;</td><td><input class='input' type='submit' name='submit' value='Log in' /></td>n
</tr>n
</form>n
</table><br >n
<a href='register.php'>Register</a>";
} else {
echo "Welcome $myusername</p>n
<p style='text-align:right; margin-right: 30px'>n
<a href='userdetails.php'>My Details |</a>n
<a href='logout.php'>Log out |</a>n";
} ?>
<a href='userlist.php'>Userlist |</a>
</p>
[/code]


the session_destroy(); will destroy your session and you can have a fresh start.
Copy linkTweet thisAlerts:
@DoppleauthorMar 06.2006 — No, checklogin.php is below. Would it actually be in this file I should put the lines then? Also where, If I destroy the session, would I create it again
[code=php]<?php
include("misc.inc");
$tbl_name="members";

// Connect to server and select databse.
mysql_connect("$host", "$user", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");

$mypassword = sha1($mypassword);
$myusername = addslashes($myusername);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
$_SESSION['myusername'] = stripslashes($_POST['myusername']);
$_SESSION['mypassword'] = $_POST['mypassword'];
if(isset($_POST['remember'])){
setcookie("cookname", $_SESSION['myusername'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['mypassword'], time()+60*60*24*100, "/");
}

// Register $myusername and redirect to file "login_success.php"

session_register("myusername");
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
?>[/code]
Copy linkTweet thisAlerts:
@geomirMar 07.2006 — ? Hi,

By using the last code I posted you should not have a problem even if the user did not use cookies.

The session is created when you use the session_start() or you register a variable, it must be though before any output is sent to the browser.

Also do not put the lines in the checklogin.php

Happy coding ?
Copy linkTweet thisAlerts:
@DoppleauthorMar 07.2006 — Sorry it doesn't seem to work it won't allow me to log in once those lines are in. I think the problem may be in that login.php (which is the file containing the form) is included in my index.php (shown below). Also when I use your code it shows error "session already started but if I take out the session above it doesn't solve the problem. Should I forget about having a seperate login.php and just add the code into all the pages?
[code=php]<?php
session_start();
include( "pagehead.php" );
include( "login.php" );
include( "misc.inc" );
echo "<h1>Guestbook</h1>n
<p>Please select one of the boards below.</p>n";
mysql_connect("$host", "$user", "$password");
mysql_select_db("$database");
$result = mysql_query("SELECT ID, Name FROM mblist ORDER BY Name;");
if (!mysql_num_rows($result)) {
echo "<p>There seems to be a problem. Check back later.</p>n";
} else {
while ($row = mysql_fetch_assoc($result)) {
extract($row, EXTR_PREFIX_ALL, "mb");
echo "<p class='gbindex'><a href="mbindex.php?MBID=$mb_ID">$mb_Name</a>";
extract(mysql_fetch_array(mysql_query("SELECT Count(ID) AS NumThreads FROM mbmsgs WHERE MBID = $mb_ID AND Parent = 0;")), EXTR_PREFIX_ALL, "mb");
extract(mysql_fetch_array(mysql_query("SELECT Count(ID) AS NumPosts FROM mbmsgs WHERE MBID = $mb_ID;")), EXTR_PREFIX_ALL, "mb");
echo "tThreads: $mb_NumThreads, Posts: $mb_NumPosts</p><br />n";
}
}
?>
<hr />
<p><a href="search.php">Search</a> the forum.</p>
<p><a href="instructions.php">Insructions</a> for the board.</p>
</div>
</body>
</html>[/code]
×

Success!

Help @Dopple 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.27,
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,
)...