ckerr27 Posted March 26, 2012 Share Posted March 26, 2012 Hey, i have created a website an have a login feature working with username and password from a mysql database. I have been trying to get the username to appear on the home page when the user logs in e.g. Welcome ckerr27 Any help would be much appreciated! Thanks Connor Link to comment https://forums.phpfreaks.com/topic/259743-help-please/ Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 What does the code look like? Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331224 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $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 $username and $password, table row must be 1 row if($count==1){ // Register $username, $password and redirect to file "home.html" session_register("myusername"); session_register("mypassword"); header("location:home.html"); } else { echo "Wrong Username or Password"; } ?> This is the checklogin.php file. when the login has been aproved the user is redirected to the home.php page Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331288 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 So you will want to ad the username onto the querystring if the page to be redirected to: header("location:home.html?username=" . $myusername); then in home.php you will grab the value using $_GET['username']; Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331292 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 Thanks, I put this code into checklogin.php if($count==1){ // Register $username, $password and redirect to file "home.html" session_register("myusername"); session_register("mypassword"); header("location:home.php username=" . $myusername); } Getting this error message The requested URL /website/home.php username=admin was not found on this server. Sorry its probably easy fixed Thanks again Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331295 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 You don't want to be stripping slashes from a password as it could contain a slash. Also, if you store your passwords as a hash you don't have to worry about SQL Injection from the password POV as it'll get hashed when you check it. Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331301 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 you have a space in the URL instead of a question mark (?). The question mark starts the query string of the URL. Use: header("location:home.php?username=" . $myusername); NOT header("location:home.php username=" . $myusername); Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331303 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 ok now when i login and try to grab the username I am getting this printed on the home.php screen $_GET['username']; Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331306 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 post the relevant code. Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331308 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 <html> <head> <link rel="stylesheet" type ="text/css" href="anything2.css" </head> <body> <div id="container"> <div id="header"><img src="imagesb.jpg" alt="Cool Image" align="left"> <img src="images.jpg" alt="Cool Image" align="right"><center><b><font size="6.5"><br><br>G.O.A.C.A Home Page</b></center></font> </div> <div id="leftnav"><center> $_GET['username']; <br><br> <input type= "button" style="width:120px;" value="Personal Details" onClick="window.location= 'personaldetails.php' "> <br><br> <input type= "button" style="width:120px;" value="Club Details" onClick="window.location= 'clubdetails.php' "> <br><br> <input type= "button" style="width:120px;" value="Future Events" onClick="window.location= 'futureevents.php' "> <br><br> <input type= "button" style="width:120px;" value="News" onClick="window.location= 'news.php' "> <br><br> <input type= "button" style="width:120px;" value="FAQ" onClick="window.location= 'faq.php' "> <br><br> <input type= "button" style="width:120px;" value="Wall" onClick="window.location= 'wall.php' "> <br><br> <input type= "button" style="width:120px;" value="About Us" onClick="window.location= 'about.php' "> </div> <div id="body"> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <A HREF = index.php>Log out</A> <div id="footer">Welcome to the Glens of Antrim Chess Association Homepage!!!!</div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331309 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 <div id="leftnav"><center> <?php echo $_GET['username']; ?> <br><br> Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331312 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 Im gettig root for every login, i thonk its to do with this line of code in the checklogin.php $username="root"; // Mysql username When i delete the root it does not display anything Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331313 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 well you are sending $myusername to home.php, which comes from this line in the checklogin page: $myusername=$_POST['myusername']; So it should contain the username that the user enters. Check to see if register_globals is turned on, place this code at the top of the page. <?php echo "Register Globals: " . ini_get('register_globals'); ?> Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331318 Share on other sites More sharing options...
ckerr27 Posted March 26, 2012 Author Share Posted March 26, 2012 It just returns at the top of the page Register Globals: Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331320 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 It just returns at the top of the page Register Globals: Look at this bug report https://bugs.php.net/bug.php?id=34536 It should be returning a value. As a quick fix, change the header to header("location:home.php?myusername=" . $myusername); and grab the value in home.php using $_POST['myusername']; Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331355 Share on other sites More sharing options...
ckerr27 Posted March 27, 2012 Author Share Posted March 27, 2012 I attatched the error i recieve when i enter the above code. Thanks for your help Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331541 Share on other sites More sharing options...
seanlim Posted March 27, 2012 Share Posted March 27, 2012 I might be totally wrong here, but I don't see a point of passing the username through a get request if you are already storing it in a session variable! Instead of session_register, do a $_SESSION['myusername'] = $myusername; and on the second page, just do <?php echo $_SESSION['myusername']; ?> No need to play with GET requests! Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331548 Share on other sites More sharing options...
ckerr27 Posted March 27, 2012 Author Share Posted March 27, 2012 That worked perfectly thanks. I kept the session register but the echo worked perfectly. now when i try to log out and login with a different user the first username is still displayed. I have a logout function... <?PHP session_destroy(); ?> Is this the correct code to use when the logout link is pressed? Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331550 Share on other sites More sharing options...
seanlim Posted March 27, 2012 Share Posted March 27, 2012 see the notes on session_destroy, the example there tells you exactly what you need to do to remove global session variables. Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331553 Share on other sites More sharing options...
ckerr27 Posted March 27, 2012 Author Share Posted March 27, 2012 Sorted thanks alot!! Link to comment https://forums.phpfreaks.com/topic/259743-help-please/#findComment-1331559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.