andy_b_1502 Posted September 22, 2011 Share Posted September 22, 2011 I would like to know how to take details of my 'users' table from mysql database and check if that user is logged in using cookies or sessions somehow and have that users username displayed on my site, something like: user logged in as: (username), how is this done? What would you guys need to see in order for you to help me? im guessing the login and register scripts which are below, if you need anymore let me know, thanks. Login script: <?php if(isset($_POST['submit'])){ //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ) { header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "myserver", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "removalspacelogin", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '$username' and upassword = '$password'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ) { header( "Location:login_success.php" ); } else #or return to: login.php { header( "Location:$referer" ); exit(); } } ?> <?php ini_set("display_errors","ON"); ?> <html> <head><title>Check-login</title></head> <body> <?php echo( $msg ); ?> </body> </html> Register users script: <?PHP $user_name = "removalspace"; $password = "123"; $database = "removalspacelogin"; $server = "removalspacecom.ipagemysql.com"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "INSERT INTO users (username, upassword, email) VALUES ('" .$username. "', '" .$upassword. "', '" .$email. "')"; $result = mysql_query($SQL); header( 'Location: http://www.removalspace.com/index.php' ); exit(); } else { print "Database NOT Found "; mysql_close($db_handle); } ?> Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/ Share on other sites More sharing options...
AyKay47 Posted September 22, 2011 Share Posted September 22, 2011 basically, once you have determined that the $rs variable which stores your query is returning true.. you can set session variables to either the username in the db or the user typed username that matches the db username.. some thing like if($rs){ $_SESSION['username'] = $username; } note: make sure that session_start is at the top of each script that you are planning on using this session or any other session for that matter Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271678 Share on other sites More sharing options...
andy_b_1502 Posted September 22, 2011 Author Share Posted September 22, 2011 Thanks, where does that if statement go? Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271681 Share on other sites More sharing options...
AyKay47 Posted September 22, 2011 Share Posted September 22, 2011 in your login script right below where you set your $rs variable... Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271682 Share on other sites More sharing options...
andy_b_1502 Posted September 23, 2011 Author Share Posted September 23, 2011 Thanks, and then put: session start () at the top of every page that i need their username to be displayed? Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271939 Share on other sites More sharing options...
voip03 Posted September 23, 2011 Share Posted September 23, 2011 Before you start using PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. <?php session_start(); // start up your PHP session! ?> Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271941 Share on other sites More sharing options...
andy_b_1502 Posted September 23, 2011 Author Share Posted September 23, 2011 When using <?php session_start(); include(db.php login.php) ?> I get: "Parse error: syntax error, unexpected T_STRING in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 3" With out the in include i get: Warning: session_start() [function.session-start]: open(/home/users/web/b109/ipg.removalspacecom/cgi-bin/tmp/sess_01d911f0daf2192c48ae1f477c853e07, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 Although this is displayed on top of my website page? Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271947 Share on other sites More sharing options...
voip03 Posted September 23, 2011 Share Posted September 23, 2011 your code include(db.php login.php) you need to place the code inside the quotation mark. Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271948 Share on other sites More sharing options...
andy_b_1502 Posted September 23, 2011 Author Share Posted September 23, 2011 <?php session_start(include('db.php' 'login.php')); ?> error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in *** on line 2 Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271949 Share on other sites More sharing options...
voip03 Posted September 23, 2011 Share Posted September 23, 2011 you can not call 2 file in one go you need coma. Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271953 Share on other sites More sharing options...
andy_b_1502 Posted September 23, 2011 Author Share Posted September 23, 2011 I think i need to go back to square one with this? Here is whats at the top of my pages that i want the log-in check to do and display the username if there logged in: <?php session_start(); include("check_login2.php"); echo ("message!!!"); ?> and this is "check_login2.php with the suggested new $rs if statment: <?php if(isset($_POST['submit'])){ //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ) { header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "server", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "removalspacelogin", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '$username' and upassword = '$password'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); if($rs){ $_SESSION['username'] = $username;} #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ) { header( "Location:login_success.php" ); } else #or return to: login.php { header( "Location:$referer" ); exit(); } } ?> <?php ini_set("display_errors","ON"); ?> <html> <head><title>Check-login</title></head> <body> <?php echo( $msg ); ?> </body> </html> im getting error messages with these in browser: Warning: session_start() [function.session-start]: open(/home/users/web/b109/ipg.removalspacecom/cgi-bin/tmp/sess_11060ed30d8458b614a7263f3d65fb48, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/advertise.php on line 2 message!!! Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271955 Share on other sites More sharing options...
voip03 Posted September 23, 2011 Share Posted September 23, 2011 1st try this code. once you happy with it then start your one. <?php if (isset($_POST['submitted']) && !empty($_POST['myusername']) && !empty($_POST['mypassword'])) { //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // NOTE - quick check $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; // remove '/* */ ' when the st test result is OK. /* // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ){ header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "server", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "removalspacelogin", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '". $username."' and upassword = '".$password."'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); if($rs){ $_SESSION['username'] = $username;} #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ){ header( "Location:login_success.php" ); } else { header( "Location:$referer" ); exit(); }} */ } else { ?> <?php ini_set("display_errors","ON"); ?> <html><head><title>Check-login</title></head> <body> <?php echo( $msg ); ?> <form action="#" method="post"> <fieldset> <legend>Login</legend> <p>Username: <input name="myusername" type="text" size="60" maxlength="100" /></p> <p>Password: <input name="mypassword" type="password" size="60"/></p> <!-- The hidden input here is a trick I use to check for a form’s submission. Doing this is sometimes necessary as just pressing Enter within Internet Explorer for Windows will submit a form withoutever setting the $_POST['submit']variable. --> <input name="submitted" type="hidden" value="true" /> <input name="submit" type="submit" value="Login" /> </form> </body> <? } ?> </html> Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1271958 Share on other sites More sharing options...
andy_b_1502 Posted September 23, 2011 Author Share Posted September 23, 2011 Thank you, when logging in a fake user (Mary) i get error message: Hello mary Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/check_login2.php:9) in /hermes/bosweb25a/b109/ipg.removalspacecom/check_login2.php on line 31 Aslo what would i have to change to fit this code into my desgined page rather than a blank white page? Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1272173 Share on other sites More sharing options...
andy_b_1502 Posted September 24, 2011 Author Share Posted September 24, 2011 This is the problem, i had a blank page working before using a script i pulled from another site, that worked but it looked like it didnt belong to my website. I need the code you suggested to work without the user being taken to a blank page and just use my existing form on "login.php" What do i need to take away from your script for it to used with an existing form? Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1272294 Share on other sites More sharing options...
voip03 Posted September 24, 2011 Share Posted September 24, 2011 1. read this topic http://www.phpfreaks.com/forums/index.php?topic=37442.0 2. place this code top of the code /* "Warning: Cannot modify header information - headers already sent by " To avoid the header error , give value zero to $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; */ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; ob_start(); 3.Open the windows and shout ' header sucks! ' AND start Re arranging the logics and code. Link to comment https://forums.phpfreaks.com/topic/247638-remember-user-login-with-geeting/#findComment-1272347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.