s0c0 Posted January 31, 2007 Share Posted January 31, 2007 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> </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? Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/ Share on other sites More sharing options...
KrisNz Posted January 31, 2007 Share Posted January 31, 2007 try calling [code]session_write_close();[/code] before you do your redirect. Even if thats not the problem you should do it anyway. :) Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-173484 Share on other sites More sharing options...
s0c0 Posted January 31, 2007 Author Share Posted January 31, 2007 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. Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-173487 Share on other sites More sharing options...
s0c0 Posted January 31, 2007 Author Share Posted January 31, 2007 any help on this?bump... Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-173804 Share on other sites More sharing options...
s0c0 Posted January 31, 2007 Author Share Posted January 31, 2007 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.htmlThe 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? Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-173938 Share on other sites More sharing options...
s0c0 Posted February 1, 2007 Author Share Posted February 1, 2007 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] Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-174209 Share on other sites More sharing options...
s0c0 Posted February 6, 2007 Author Share Posted February 6, 2007 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. Link to comment https://forums.phpfreaks.com/topic/36452-solved-problem-with-passing-session-using-session-functions-error-undefined-index/#findComment-178447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.