Jump to content

Remember user login with geeting.


andy_b_1502

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.