woodplease Posted November 30, 2009 Share Posted November 30, 2009 hi. as you will soon find out, i'm pretty useless at php. i need help creating a session to store a users name. i just need a simple text box where the user can enter his or her name and press an enter site button or something. i also want to be able to display the users name on each page after that. i know how to open a session, its just making it so that the user enters their name i cant do. i have sorted out the logout page( a bit backwards i know). its just creating the initial session i cant seem to do. Any help would be great. thanks Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/ Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Share Posted November 30, 2009 Assuming you know how to make an html form that accesses a php file I will write this... $_SESSION['USERNAME'] = $_POST['txtboxcontrol']; of course replace the txtboxcontrol with whatever the id of the form control is where they are entering their name Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968177 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 Thanks for replying so quickly. This is what i've done now <form action="<?php $_SESSION['USERNAME'] = $_POST['username']; ?>" method="post"> <input type="text" name="name" id="username"> <input type="submit" name="submit" value="Enter your name"> </form> If this is correct. how can i get it to display the username on other pages. i've tried this but it doesnt seem to work <?php echo "User : ".$_SESSION['USERNAME']; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968194 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Share Posted November 30, 2009 Can you just throw the php code directly into the quotations like that in your form element tag? I didn't know you could... If you can't then that's the problem change your name attribute to username on the textbox Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968196 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 i've been told you can, if not how else could i do it? Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968201 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Share Posted November 30, 2009 i've been told you can, if not how else could i do it? try changing the name attribute first and then if that doesn't work then you can't use php code in there... if it does then you can. But the post has to read from the name attribute so thats one problem. Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968203 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 no, that hasnt done anything, any other ideas would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968213 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Share Posted November 30, 2009 Take that php code out of the form is my advice. Make the form either submit to a new php file, like an execute php file where you take the post variable or use the isset function on that same file to check and see if the submit button is pressed with the following isset($_POST['submit']); Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968224 Share on other sites More sharing options...
Jnerocorp Posted November 30, 2009 Share Posted November 30, 2009 use this code it has logout feature and everything <?php session_start() if(isset($_POST['username'])) { $_SESSION['username'] = S_POST['username']; echo "You are now logged in"; } if(isset($_GET['logout']) { $logout = $_GET['logout']; if($logout == "logout") { $_SESSION = null; unset($_SESSION['username']) } } ?> <html> <head> <title> Login System </title> </head> <body> <?php if(isset($_SESSION['username'])) { echo "Welcome ".$_SESSION['username']." to our site."; } ?> <br> <center> <h1>Website</h1> </center> <?php if(!isset($_SESSION['username'])) { ?> <form action="" method="post"> Username: <input type="text" name="username"><br> <input type="submit" value="login"> </form> <?php } ?> <br><br> <?php if(isset($_SESSION['username'])) { ?> <form action="" method="get"> <input type="submit" name="logout" value="Logout"> </form> <?php } ?> </body> </html> -John Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968229 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 Hey thanks for the code, when i view the page with this code, i keep getting this error: Parse error: syntax error, unexpected T_IF in /berw/ugrad1/base/a/agd8/public_html/gameshack/login.php on line 4 Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968242 Share on other sites More sharing options...
MisterWebz Posted November 30, 2009 Share Posted November 30, 2009 Hey thanks for the code, when i view the page with this code, i keep getting this error: Parse error: syntax error, unexpected T_IF in /berw/ugrad1/base/a/agd8/public_html/gameshack/login.php on line 4 Any ideas? Thanks You forgot the ';'. <?php session_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968248 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 Not to mention the $ instead of S here: $_SESSION['username'] = S_POST['username']; Should be: $_SESSION['username'] = $_POST['username']; Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968252 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 now i seem to be getting another problem Parse error: syntax error, unexpected '{' in /berw/ugrad1/base/a/agd8/public_html/gameshack/login.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968254 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Share Posted November 30, 2009 now i seem to be getting another problem Parse error: syntax error, unexpected '{' in /berw/ugrad1/base/a/agd8/public_html/gameshack/login.php on line 11 Post the code on your page Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968259 Share on other sites More sharing options...
woodplease Posted November 30, 2009 Author Share Posted November 30, 2009 the code..... <?php session_start(); if(isset($_POST['username'])) { $_SESSION['username'] = $_POST['username']; echo "You are now logged in"; } if(isset($_GET['logout']) { $logout = $_GET['logout']; if($logout == "logout") { $_SESSION = null; unset($_SESSION['username']) } } ?> <html> <head> <title> Login System </title> </head> <body> <?php if(isset($_SESSION['username'])) { echo "Welcome ".$_SESSION['username']." to our site."; } ?> <br> <center> <h1>Website</h1> </center> <?php if(!isset($_SESSION['username'])) { ?> <form action="" method="post"> Username: <input type="text" name="username"><br> <input type="submit" value="login"> </form> <?php } ?> <br><br> <?php if(isset($_SESSION['username'])) { ?> <form action="" method="get"> <input type="submit" name="logout" value="Logout"> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968262 Share on other sites More sharing options...
Jnerocorp Posted November 30, 2009 Share Posted November 30, 2009 here you go: <?php session_start(); if(isset($_POST['username'])) { $_SESSION['username'] = $_POST['username']; echo "You are now logged in"; } if(isset($_GET['logout'])) { $logout = $_GET['logout']; if($logout == "logout") { $_SESSION = null; unset($_SESSION['username']); } } ?> <html> <head> <title> Login System </title> </head> <body> <?php if(isset($_SESSION['username'])) { echo "Welcome ".$_SESSION['username']." to our site."; } ?> <br> <center> <h1>Website</h1> </center> <?php if(!isset($_SESSION['username'])) { ?> <form action="" method="post"> Username: <input type="text" name="username"><br> <input type="submit" value="login"> </form> <?php } ?> <br><br> <?php if(isset($_SESSION['username'])) { ?> <form action="" method="get"> <input type="submit" name="logout" value="logout"> </form> <?php } ?> </body> </html> -John Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968410 Share on other sites More sharing options...
gergy008 Posted November 30, 2009 Share Posted November 30, 2009 Why is there multiple pages >_> Another stupid reply!! Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968536 Share on other sites More sharing options...
Jnerocorp Posted December 1, 2009 Share Posted December 1, 2009 @gergy008: who is ur reply at? Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968611 Share on other sites More sharing options...
aeroswat Posted December 1, 2009 Share Posted December 1, 2009 Line 11, this line if(isset($_GET['logout']) { is missing a end parentheses or a ')' after the get Quote Link to comment https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/#findComment-968928 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.