ow-design Posted April 24, 2007 Share Posted April 24, 2007 Hi, I am 16yrs old and need some help with finsihing a php site i am building. Basically at the moment i have a login form which check users details etc etc and creates a cookie based on username to dsiplay throughout the site. But how do i get their real name from the database with just a username and password from loigin form? Could somebody pist some code pleae Thank You -- I have looked everywhere and i bet its really simple too but i am stuck and need some help. Quote Link to comment Share on other sites More sharing options...
JayLewis Posted April 24, 2007 Share Posted April 24, 2007 it depends on how you have set it up. I suggest using sessions instead of cookies. Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 I can never get sessions to work, and it is a lot more complicated than cookies isnt it? it only needs to be really simple. Can you give me rough guidance on how i would do it. Like how to get other data from a table row by username? Thanks Quote Link to comment Share on other sites More sharing options...
JayLewis Posted April 24, 2007 Share Posted April 24, 2007 post your script up. or add me on msn Jay_Lewis@Jaydiocity.co.uk Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 sessions and cookies are very similar... cookies store on the computer, sessions store on the server that much said... cookies cannot contain arrays so easily... sessions can... when you do the login, just store their data into $_SESSION ($_SESSION[user]=$row) then anywhere on the page, you can use any of that information via $_SESSION[user][fullname]... for example :-) Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 Okay I have a login page: login.php which simply takes username & password and sends it to checklogin.php... Code below. See at the moment i am using cookies to store username and display that so on the pages it says welcome ________ (gets cookie name) but i would really like it to say welcome ____ (real name) Code below...Thanks! <?php setcookie("user", $_POST['username'], time()+3600); $host="localhost"; // Host name $username="myusername"; // Mysql username $password="mypassword"; // Mysql password $db_name="raphkat.co.uk_cart"; // Database name $tbl_name="users"; // 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 signup form $username=$_POST['username']; $password=$_POST['password']; $sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; $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 "login_success.php" session_register("username"); session_register("password"); header("location:index.php"); } else { echo "You have entered an incorrect username... Please try again"; } ?> Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 too complicated... <?php $host="localhost"; // Host name $username="myusername"; // Mysql username $password="mypassword"; // Mysql password $db_name="raphkat.co.uk_cart"; // Database name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $username=addslashes(strip_tags($_POST['username'])); $password=md5($_POST['password']); $result=mysql_query("SELECT * FROM `users` WHERE `username`='$username' and `password`='$password' LIMIT 1"); $row=mysql_fetch_array($result); if(!empty($row)){ $_SESSION[user]=$row; header("location:index.php"); }else{ echo "You have entered an incorrect username... Please try again"; } ?> Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 Okay thanks for ammendments Traith... but what now how do i solve my problem? Thanks. Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 with that script, it saves all your user info into the $_SESSION[user]... so if you want their name somewhere on the page... just echo $_SESSION[user][fullname]; or the likes... Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 Thanks Taith, the code looks good but now it will not recognise any usernames and will reject them all as being incorrect. Any ideas what the problem might be? Thanks. Ollie Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 Okay the script seems to be working now but the code that gets the session data is not displaying anything? Whats wrong. Thanks Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 did you start your sessions? at the top of every file... put "session_start()" Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 Okay no i forgot that... But now i am getting this message Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/raphkat/public_html/about.php:6) in /home/raphkat/public_html/about.php on line 17 HELP!!! Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 its not at the top of your page... in about.php on line 17, something has already been output to the browser...put session_start(); on the very top of EVERY page Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 BEFORE you type anything else (except "<?php" of course) and dude, GOOGLE IS YOUR FRIEND Quote Link to comment Share on other sites More sharing options...
ow-design Posted April 24, 2007 Author Share Posted April 24, 2007 YER IT IS! Okay so no error messages now but... still no sign of any data on the page with,,, <?php echo $_SESSION[user][firstname]; ?> Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 post the page you are setting the session vars and post the page thats using the session vars Quote Link to comment 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.