CyRus2273 Posted November 20, 2011 Share Posted November 20, 2011 Hi guys I am new to PHP and need som help. I have set up a site that allows a user to log in through a simple form where the data is then send to checklogin.php. Here the data is checked up against my sql database and if the login is correct the user is transfered to the "secret" members only site. All this works fine. My question is then, how do I get the members site to greet the member with "Hello 'username'"; of course where the username changes depending on the login. This is the part where the username and password is checked: <?php $host="mydbb10.surftown.dk"; // Host name $username="****"; // Mysql username $password="****"; // Mysql password $db_name="****"; // 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 $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "korrekt.php" session_register("myusername"); session_register("mypassword"); echo "Tak fordi du loggede ind<br>Redirecter..."; header("location: ../forhandlerservice.php"); } else { echo "Forkert brugernavn eller password"; header("location: ../loginfejl.html"); } ?> and this is the first line of code on the members site: <? session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?> Sorry if I provided to much code, just want to make sure that I don't forget anything. Any help is appreciated. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/ Share on other sites More sharing options...
btellez Posted November 20, 2011 Share Posted November 20, 2011 Looks like your using sessions already, try this: // On the page where you check for correct log in information session_start(); $_SESSION['username'] = 'actualUsername'; // On the page where you want to use the username session_start(); echo "Hello". $_SESSION['username']; Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289748 Share on other sites More sharing options...
CyRus2273 Posted November 20, 2011 Author Share Posted November 20, 2011 Thanks for the reply. I actually figured it out by adding this block of code: <?php echo "Hello " . $myusername; ?> , so that was basically the same you suggested. My next question is then, how do I register something in the session that is not contained in the form. Meaning that in my database I have username, password and real name. It would obviously look better if I could get the code to print out "Hello "Real name"". I hope it makes sense, and again, thanks for your answer. Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289753 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 You can fire a query and then store your desired thing in a variable and then you can use session_register(); Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289755 Share on other sites More sharing options...
CyRus2273 Posted November 20, 2011 Author Share Posted November 20, 2011 How would I go about this? I'm pretty new to html and php so sorry if I ask stupid questions Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289758 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 what do you want to register in the session Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289759 Share on other sites More sharing options...
CyRus2273 Posted November 20, 2011 Author Share Posted November 20, 2011 In my database I have username, password, email and real name. I assume I have to register username and password in order to verify the user is logged in. Furthermore I would like to register real name so that I can print out: Hello "Real name". Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289760 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 The way to Register session <?php // Use of session_register() is deprecated $barney = "A big purple dinosaur."; session_register("barney"); // Use of $_SESSION is preferred, as of PHP 4.1.0 $_SESSION["zim"] = "An invader from another planet."; // The old way was to use $HTTP_SESSION_VARS $HTTP_SESSION_VARS["spongebob"] = "He's got square pants."; ?> Now use sql query like $query = 'SELECT username,password,realname FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin $result = mysql_query($query , $db); $row = mysql_fetch_assoc($result); $us = $row['username']; $pa = $row['password']; $rn = $row['realname']; Now use these variables to register as shown above. Hope this would help if not i am here i will explain more Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289761 Share on other sites More sharing options...
CyRus2273 Posted November 20, 2011 Author Share Posted November 20, 2011 Thanks, but I'm pretty lost on the whole register thing Given the code I provided how would I go about implementing it there. The thing is this. I have one page with a form requiring username and password that when submitted posts the data to checklogin.php. The username and password is then checked against my database and if correct the user is redirected to the member site, where the first line of code is: <? session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?> Following this php code is a whole bunch of html showing the actual member site. Inside this html I have then added: <div id='splitter2'> <?php echo "Hello " . $myusername; ?> </div> , which at the current time prints out: Hello test123 (e.g.). I would then like it to instead of printing out the username which is usually something corny I would like it to print out Hello John (e.g.), where John in the database is associated with the username test123. Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289766 Share on other sites More sharing options...
btellez Posted November 20, 2011 Share Posted November 20, 2011 to follow up using killervastu's code: use this code on the page you plan on using the real name $query = 'SELECT `realname` FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin $result = mysql_query($query , $db); $row = mysql_fetch_assoc($result); $realName = $row['realname']; // sotre it in your session if you want to use it elsewhere... session_register("realName"); // Further down somehwere on your page:... echo "Hello ". $realName; Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289770 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 when the username and password is correct use this in login.php if username and password match then $sql = mysql_query("SELECT * FROM yourtable WHERE uniqueid =".$username." AND password ='".$password."'" ,$db) or die(mysql_error()); $row = mysql_fetch_array($sql) or die(mysql_error()); $usernamec = $row['uniqueid']; $name = $row['name']; $_SESSION['uniqueid'] = $usernamec; $_SESSION['logged'] = 1; $_SESSION['name'] = $name; now in member .php use this <?php session_start(); if(!isset($_SESSION['logged']) || $_SESSION['logged'] != 1) { header('Refresh: 5; URL=login.php?redirect='. $_SERVER['PHP_SELF']); echo '<p>You will be Redirected to login page within 5 sec.</p>'; echo '<p> IF the browser Dose not redirect <a href="login.php?redirect='. $_SERVER['PHP_SELF'] . '">Click here</a></p>'; die(); } ?> instead of this <? session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?> now you can easily display the name using echo 'Welcome '.$_SESSION['name']; Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289771 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 to follow up using killervastu's code: use this code on the page you plan on using the real name $query = 'SELECT `realname` FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin $result = mysql_query($query , $db); $row = mysql_fetch_assoc($result); $realName = $row['realname']; // sotre it in your session if you want to use it elsewhere... session_register("realName"); // Further down somehwere on your page:... echo "Hello ". $realName; You can do it like this also if you wish if you feel not to use session_register() use the above post's script Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289772 Share on other sites More sharing options...
PFMaBiSmAd Posted November 20, 2011 Share Posted November 20, 2011 session_register was depreciated over 9 years ago (in favor of $_SESSION variables), finally throws a depreciated error messages in php5.3, and has been removed as of php 5.4. DON'T use session_register in your code and any code that current uses it must be updated to use $_SESSION variables. Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289775 Share on other sites More sharing options...
CyRus2273 Posted November 20, 2011 Author Share Posted November 20, 2011 Thanks for all the answers, but I must be stupid. I can't figure this out. The three pages I have in this loop are: login.html (with the form), checklogin.php (where the data from the form is send to and checked against the database) and members.php (where the user is send to if login is correct - otherwise user is send back to login.html). I tried adding the code that you suggested but quite honestly I didn't know what segments to take out and replace with your code. I really appreciate your help guys but I really suck at php so please bare with me If you guys wouldn't mind if would be easier for me to see my own code altered instead of correct code posted as I don't know where to put it in Thanks again, and sorry for not understanding Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289785 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 hey pm me your mail id i will send you the complete code Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289791 Share on other sites More sharing options...
Pikachu2000 Posted November 20, 2011 Share Posted November 20, 2011 The code you posted is obviously from phpeasystep.com, and it is obsolete and outdated. I would not using that site to attempt to learn. Their code is at least 7-8 years behind the curve, and will quite likely cease to function at all with future versions of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/#findComment-1289815 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.