Jump to content

[SOLVED] Problem with passing session using session functions, error: undefined index.


s0c0

Recommended Posts

I am building a login page, once the user has authenticated against the mysql databse the user is taken to a page with whatever they are storing in the mysql table.  The login page should pass the username using the $_SESSION['user'] array to the next page.  Instead I get the following error:

[quote]Undefined index: user in /srv/www/htdocs/mymusic.php on line 10[/quote]

Here is the form code on the first page login.php:

[code]
<form method="post" action="auth.php">
<table>
<tr>
<td>Username</td><td><input name="username" type="text"></td></tr>
<tr>
<td>Password</td><td><input name="password" type="text"></td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="Submit" value="Login" /></td></tr>

</html>
[/code]

Here is the code on the second page auth.php:

[code]
<?php

$username = $_POST['username'];
$password = $_POST['password'];

$con = mysqli_connect('localhost', 'www', 'somepass', 'music'); /* connect to database */

        if (!$con){
      die('Could not connect: ' . mysql_error()); /* report connection error on failure */
        }

$con->select_db("music"); /* select database */
$auth_query = "Select * FROM users WHERE username='". $username. "' and password='" .$password. "'"; /* auth query to ivault.users */
$auth = mysqli_query($con, $auth_query);
$row = mysqli_fetch_row($auth);
$count = $row[0];

        if ($row > 0){ /*  login success */
                $_SESSION['user'] = $username; /* create session cookie */
                header("Location: http://192.168.1.134/mymusic.php"); /* browser redirect */
        }
        else { /* Login failure */
                echo "We're sorry ". $username ." but we were unable to log you in using password ". $password .". Please try again</p>";
                echo "<a href=\"http://192.168.1.134/login.php\">Retry login</a>";
        }

mysqli_close($con); /* close connection */
exit;

?>
[/code]

At this point I can comment out the http redirect and echo $_SESSION['user'] to the auth.php page and the correct username is there.  But when I uncomment it and it redirects to the mypage.php page is when I get the error.  Here is the php code for mypage.php:

[code]
<?php
session_start();
$mypage_user = $_SESSION['user'];

echo "<p>Thank you for logging in ". $mypage_user ."</p>";
echo"<p><a href=\"upload.php\">Add a Song</a></p>";

$db = mysqli_connect('localhost', 'www', 'layer3', 'music'); /* connect to database */
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to the database.';
exit;
}
?>
[/code]

Any help would be awesome as I am stumped.  The auto start session value in php5.ini is set to 0 and I can't see anything else in there that should be causing this.

Bonus unrelated question:  Should I be using mysqli or plain mysql php connector?
I added that to the auth.php page right before the redirect but it made no difference.  I've actually commented out the redirect in place of a link that echo's on to auth.php so I can echo the $_SESSION['user'] array which works.  I don't get it, I'm following the instructions in this book to a T as far as I can see.
Do I need to put the php code in before the html header?  That's what it looks like based on this sticky thread http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

The problem with that is I use a loop to draw the html tables based on what the query returns.  I don't think I can put <?php ?> then html followed by more <?php ?> can I?
Thankfully I'm persistent and don't give up easily.  I figured out the problem myself. It looks like PHP5 wants to see double quotes and not single quotes for $_SESSION[].

Good:

[quote]$_SESSION["user"][/quote]

Bad:

[quote]$_SESSION['user'][/quote]
Another thing I found is sessions seem to work better if you carry the session using an fqdn or ip.  I experienced problems for instance when going from localhost/page1.php to localhost/page2.php, once I changed the link to mylocalip/*.php everything worked better.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.