tommyboy123x Posted December 1, 2006 Share Posted December 1, 2006 I am having trouble with basic PHP sessions - I've checked out all the tutorials and everything but just can't figure out what else to do or what is wrong...signin.php[code]<?php include("head.html"); ?></center></font></b><font size="3" color="red"><?php$error = $_GET['error'];$signin = $_GET['signup'];if ($error == invalid){ echo 'Invalid password/username combination'; }if ($signin == true){ echo 'Congratulations! You have successfully signed up!'; }?></font><b><font size="4"><center>Sign-in <?php include("font.html"); ?><br><br>Type in your login information. If you do not yet have an account, please <a href="signup.php">request an invite</a>.<br><br><form method="POST" action="signinprocess.php">Username:<br> <input type="text" name="username" size="25" maxlength="20"><br>Password:<br> <input type="password" name="password" size="25" maxlength="20"><br> <input type="submit" value="Sign In" name="submit"></p></form><?php include("foot.html"); ?>[/code]signinprocess.php[code]<?phpsession_start();$_SESSION["username"] = "$username";$username = $_POST['username'];$password = $_POST['password'];// make mysql connection - the stuff below was changed on purpose for the posting of this threadmysql_connect("*SERVER*", "*USERNAME*", "*PASSWORD*") or die(mysql_error());mysql_select_db("*DATABSE*") or die(mysql_error());//must use $emailpass stored in db because password is encrypted (md5)$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";$result = mysql_query($query);//if there is anything but one unique entry - error in signinif (mysql_num_rows($result) != 1) { include("signin.php?error=invalid"); exit(); }else { header('Location: memers/index.php'); }?>[/code]and then the members index page[code]<?phpsession_start();//if there is no session variable, go to signin pageif (isset($_SESSION["username"]) == 1){ $username = $_SESSION["username"]; }else{ header ('Location: signin.php'); }include('head.html');echo $_SESSION["username"];include('font.html');include('foot.html');?>[/code]The only problem is that it does not echo the username!, yet I am still directed to the index page. Anyone know what is wrong?EDIT: this is strange... suddenly it doesnt work anymore.... when i go to sign in, at the bottom left it is requesting the page signinprocess.php, goes white for a minute, and then goes right back to the signin.php page... guess that makes it two problems ??? :(EDITEDIT: duh to the above... the index page is requesting a session variable and if it doesn't have it it means it wont let me access the page :D ::) Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/ Share on other sites More sharing options...
.josh Posted December 1, 2006 Share Posted December 1, 2006 move this:$_SESSION["username"] = "$username";to here:[code]if (mysql_num_rows($result) != 1) { include("signin.php?error=invalid"); exit(); }else { $_SESSION["username"] = $username; header('Location: memers/index.php'); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133121 Share on other sites More sharing options...
tommyboy123x Posted December 1, 2006 Author Share Posted December 1, 2006 this still doesnt work... I had it like that originally and thought it was wrong...I have a webhost (godaddy "deluxe linux") and it should all be working - but it just doesn't save the variable or something...Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133209 Share on other sites More sharing options...
projectshifter Posted December 1, 2006 Share Posted December 1, 2006 [quote author=Crayon Violent link=topic=116923.msg476680#msg476680 date=1164938998]move this:$_SESSION["username"] = "$username";to here:[code]if (mysql_num_rows($result) != 1) { include("signin.php?error=invalid"); exit(); }else { $_SESSION["username"] = $username; header('Location: memers/index.php'); }?>[/code][/quote]First, $_SESSION["username"] = "$username"; is bad. Double quotes will parse "$variables", but why bother creating the extra work? $_SESSION['username'] = $username; is all you need to do. Second, your problem is that you're assigning $username to $_SESSION['username'], except this is at the top of your script, and $username doesn't have any kind of a value to it. So you're basically saying $_SESSION['username'] = ''; Maybe you meant to put $_POST['username'] ? Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133213 Share on other sites More sharing options...
tommyboy123x Posted December 1, 2006 Author Share Posted December 1, 2006 lol thats probably why it used to sign me in... i was wondering how it could sign me in but echo no variable - must have just been echoing nothing :Peven moving that down, it still doesn't work... i appreciate the help though :)I am very new to the whole idea of sessions, but basically I'm thinking $_SESSION its similar to saying $_POST... except the variables are stored somewhere magical where you can alwasy get them..??? but heres the code with a few revisions[code]<?php//signinprocess.phpsession_start();$username = $_POST['username'];$password = $_POST['password'];// make mysql connectionmysql_connect("server", "username", "password") or die(mysql_error());mysql_select_db("database") or die(mysql_error());$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";$result = mysql_query($query);if (mysql_num_rows($result) == 1) { $_SESSION['username'] = $username; $_SESSION['password'] = $password; include('members/index.php'); }else { header('Location: signin.php?error=invalid'); exit(); }?>[/code]/members/index.php[code]<?phpsession_start();if (empty($_SESSION["username"])) { header ('Location: signin.php?error=sessionerror'); exit(); }else{ $username = $_SESSION["username"]; }include('../head.html');echo $_SESSION["username"];include('../font.html');include('../foot.html');?>[/code]i changed to empty() instead of isset() just for a test, but neither worked :-\. I also added the sessionerror part just so i could see for myself, but as of now im pretty much stuck... Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133222 Share on other sites More sharing options...
projectshifter Posted December 1, 2006 Share Posted December 1, 2006 [code]// make mysql connectionmysql_connect("server", "username", "password") or die(mysql_error());mysql_select_db("database") or die(mysql_error());$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";[/code]You put server, username and password??? Did you just remove the variables or what, because it's not going to connect like that. Also on your queries, change your queries to like msyql_query($query) or die('Error: '.mysql_error()); and it'll let you know if your SQL syntax is bad. [code]if (empty($_SESSION["username"])) { header ('Location: signin.php?error=sessionerror'); exit(); }else{ $username = $_SESSION["username"]; }[/code]Try:[code]if (!$_SESSION['username']){ header('Location: signin.php?error=sessionerror'); exit;}else echo 'User is: '.$_SESSION['username'];[/code]Let me know what you get for that. Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133227 Share on other sites More sharing options...
tommyboy123x Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=projectshifter link=topic=116923.msg476789#msg476789 date=1164952737]You put server, username and password???[/quote]lol mysql may not be my best language, but im not THAT bad at it :)I have a feeling this error is on the index.php page and not the signinprocess.php, but thats just meanyways, it still shows the same error, it won't go to the index page Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133230 Share on other sites More sharing options...
tommyboy123x Posted December 2, 2006 Author Share Posted December 2, 2006 bumb..? Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133882 Share on other sites More sharing options...
tommyboy123x Posted December 3, 2006 Author Share Posted December 3, 2006 would it be possible to POST or GET the session data?I'd really rather not use cookies because i have a mobile phone version (.mobi), and idk how cookies really work with cellphones, but there has to be SOME way of doing this!! Quote Link to comment https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-134250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.