Jump to content


Help me let users stay logged after they close the member page [Solved]


10 replies to this topic

#1 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 14 July 2007 - 01:23 PM

Hello!

I have made a registration/login system from a tutorial. The tutorial didn't include anything about sessions. Or cookies. That means every time a member logs in and closes the member page, he gets logged out.

I want to change that. I have a text:

If you have an account, please click here to log in. Administrators log in here.
If you don't have an account, please take time to register here. It is completely free!

in two pages. I want it to change to You are currently logged in. if a member has logged in, and has closed the member page/browser. And to show the text before if logged out. If you need any sources, please post that.

I looked at the tutorials in this site, but didn't find anything I would be able to understand good enough, to add where and how I need.


Thanks in advance :rolleyes:


P.S. Sorry for the off-topic, but does anyone know a good tutorial on making a members-only personal messenger? I searched without luck.

Edited by Edmachine, 18 July 2007 - 06:05 AM.


#2 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 15 July 2007 - 05:13 AM

The only thing I can think on the first part is when they log in have a field in MySql that changes to '1' and have a function that changes it back to zero after an hour or two. (I really don't think this is a good idea though so please dont take me serious because someone will come along with a much better idea)

On the messenger, I would suggest just installing some kind of tag box into a page and setting the page to something along the lines of...

if ($user == '2') {

having $user look into MySql and checkin if the user is indeed an admin.

#3 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 15 July 2007 - 11:41 AM

Uhh okay so,

Set a cookie after they login. Make it keep them logged in for x many days or whatever you would like.

#4 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 16 July 2007 - 01:03 AM

@Braunson, yeah, but how to to change a text line if user has logged in?

@Korndawg, hmm...if I fail with the above , I might try that :)

I think I am close to solving this... I found the session start thingy, in a file, so I copied to my index file, to check if user has logged in, if yes display Hello, username!, if not
If you have an account, please click here to log in. Administrators log in here.
If you don't have an account, please take time to register here. It is completely free!

So, I want to know how to display the logged user's username...
Could anyone give me info on that?

Thanks in advance!

Edited by Edmachine, 16 July 2007 - 01:20 AM.


#5 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 16 July 2007 - 02:31 AM

in the login script........

i do sumthing like

$myusername = $_POST['myusername'];
$mypassword = md5($_POST['mypassword']);

$sql  = "SELECT * FROM $table_name WHERE admin_username='$myusername' and admin_password='$mypassword'";
$result = mysql_result($sql);

$count = mysql_num_rows($result);

if($count == 1){
$_SESSION['myusername'] == $myusername;
}

that should work....

then to print username on any page just do

echo $_SESSION['myusername'];

or whatever u want to do lol.

#6 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 16 July 2007 - 02:51 AM

heres my login page if it helps

ive tried to coment it lol

<?php
	session_start();
	$pathtoroot = '';
	include($pathtoroot.'config.php');
	$page['title'] = 'Login';
	
	
	// DEBUG CLEAR SESSION
	if($_GET['debug'] == 'logout'){
		unset($_SESSION['myusername']);
		unset($_SESSION['mypassword']);
		session_destroy();
	}
	
	// CHECK FOR SESSION
	if(!empty($_SESSION['myusername']) and !empty($_SESSION['mypassword'])){
		$myusername = $_SESSION['myusername'];
		$mypassword = $_SESSION['mypassword'];
		$table_name = 'admin_users';
		$sql = "SELECT * FROM $table_name WHERE admin_username='$myusername' and admin_password='$mypassword'";
		$result = mysql_query($sql);
		// USER EXSISTS?
		$count = mysql_num_rows($result);
		if($count == 1){
			header('Location:plugin_main/index.php');
		}
	}
	
	// START LOGIN
	if($_POST['submitform'] == 'yes'){
		// TABLE NAME
		$table_name = 'admin_users';
		$myusername = $_POST['myusername'];
		$mypassword = md5($_POST['mypassword']);
		// DO THE LOGIN QUERY
		$sql = "SELECT * FROM $table_name WHERE admin_username='$myusername' and admin_password='$mypassword'";
		$result = mysql_query($sql);
		// USER EXSISTS?
		$count = mysql_num_rows($result);
		// DOES COUNT EQUAL 1?
		if($count == 1){
			$_SESSION['myusername'] = $myusername;
			$_SESSION['mypassword'] = $mypassword;
			$myfirstname = mysql_result($result,0,'admin_first_name');
			$mylastname = mysql_result($result,0,'admin_last_name');
			$myid = mysql_result($result,0,'id');
			$myfullname = $myfirstname.' '.$mylastname;
			$_SESSION['myfullname'] = $myfullname;
			$_SESSION['myid'] = $myid;
			header('Location:redirect.php');
		} else {
			$doh = 'Incorrect Login';
		}
	}
?>
<html>
	<head>
		<title><?=$config['cmsname']?> - <?=$page['title']?></title>
		<link rel="stylesheet" type="text/css" media="screen" href="<?=$pathtoroot?>css/login.css" />
	</head>
	<body>
		<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
			<input type="hidden" name="submitform" value="yes">
			<div class="login_box">
				<div class="login_question">
				</div>
				<div class="login_box_header1">
					Login: <?php if(!empty($doh)){ echo $doh; } ?>
				</div>
				<div class="login_box_header">
					Username: <br />
					<input type="text" name="myusername" /> <br />
					Password: <br /><input type="password" name="mypassword" />
				</div>
				<div class="login_button">
					<a href="#" onclick="submit();">login</a>
				</div>
			</div>
		</form>
	</body>
</html>


#7 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 16 July 2007 - 02:11 PM

Ok, so I have $_SESSION['s_username'], which I put in like this (actually I am going to give the source)
<?php
if($_SESSION['s_logged_n'] == 'true'){
?>
<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>
<body link=#000 alink=#666 vlink=#666>
<?php include 'header.php' ?>
<div id=wrapper>
<div id=head>Welcome to Techgeeks!</div>
<div id=main>Hello, <?php echo $_SESSION['s_username']; ?></div>
<br>
<div id=main>Hello, this is this site's owner <b>Edmachine</b> speaking.<br>
<br>
The site is currently under construction, but you can visit a few places.<br>
I have planed to make the following:<br>
<del>A news system</del><br>
Personal Messenger for registered members<br>
A working search engine<br>
Member profiles<br>
<br>
More to be added later.
<br>
<br>
<i>Administator - Edmachine</i>
</div>

</div>
</body>
</html>
<?php
}else {
echo '<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>
<body link=#000 alink=#666 vlink=#666>
<?php include(header.php); ?>
<div id=wrapper>
<div id=head>Welcome to Techgeeks!</div>
<div id=main>If you have an account, please click <a href=login.php>here to log in</a>. Administrators log in <a href=adminlogin.php>here</a>.<br>If you don't have an account, please take time to <a href=register.php>register here</a>. It is completely free!</div>
<br>
<div id=main>Hello, this is this site's owner <b>Edmachine</b> speaking.<br>
<br>
The site is currently under construction, but you can visit a few places.<br>
I have planed to make the following:<br>
<del>A news system</del><br>
Personal Messenger for registered members<br>
A working search engine<br>
Member profiles<br>
<br>
More to be added later.
<br>
<br>
<i>Administator - Edmachine</i>
</div>

</div>
</body>
</html>
}
?>
All should work, but I have a Parse Error at line 52, if I cut it 54... and probably so on... What is causing these parse errors? (Line 52 is in the place if user isn't logged in). I am echoing what should it display if user isn't logged in... is the way I am doing it incorrect?

#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 16 July 2007 - 02:38 PM

Rofl.

No. Either use the echo function correctly and make a full string out of that, or exit out of 'PHP mode' and simply print HTML.

There's alot of unnecessary repeated code in that by the way.

Edited by Demonslay, 16 July 2007 - 02:38 PM.


#9 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 17 July 2007 - 04:21 AM

Ok, I read some tutorials, and I am now using this thing:
<?php
 session_start();
 if(isset($_SESSION['s_username'])){
 echo "<html>
 <head>
 <title>Techgeeks.oo.lv - from websites to graphics, to music</title>
 <link href='style.css' rel='stylesheet' type='text/css'>
 <style type='text/css'>
 a:hover { background-color: #666; }
 a { text-decoration:none }
 </style>
 </head>
 <body link=#000 alink=#666 vlink=#666>
 <?php include('header.php'); ?>
 <div id=wrapper>
 <div id=head>Welcome to Techgeeks!</div>
 <div id=main>Hello, You are logged in as $_SESSION[s_username]. <a href=member.php>Click here</a> to go to your Control Panel.</div>
 <br>
 <div id=main>Hello, this is this site's owner <b>Edmachine</b> speaking.<br>
 <br>
 The site is currently under construction, but you can visit a few places.<br>
 I have planed to make the following:<br>
 <del>A news system</del><br>
 Personal Messenger for registered members<br>
 A working search engine<br>
 Member profiles<br>
 <br>
 More to be added later.
 <br>
 <br>
 <i>Administator - Edmachine</i>
 </div>
 
 </div>
 </body>
 </html>";
 
 } else {
 echo "<html>
 <head>
 <title>Techgeeks.oo.lv - from websites to graphics, to music</title>
 <link href='style.css' rel='stylesheet' type='text/css'>
 <style type='text/css'>
 a:hover { background-color: #666; }
 a { text-decoration:none }
 </style>
 </head>
 <body link=#000 alink=#666 vlink=#666>
 <?php include('header.php'); ?>
 <div id=wrapper>
 <div id=head>Welcome to Techgeeks!</div>
 <div id=main>If you have an account, please click <a href=login.php>here to log in</a>. Administrators log in <a href=adminlogin.php>here</a>.<br>If you don't have an account, please take time to <a href=register.php>register here</a>. It is completely free!</div>
 <br>
 <div id=main>Hello, this is this site's owner <b>Edmachine</b> speaking.<br>
 <br>
 The site is currently under construction, but you can visit a few places.<br>
 I have planed to make the following:<br>
 <del>A news system</del><br>
 Personal Messenger for registered members<br>
 A working search engine<br>
 Member profiles<br>
 <br>
 More to be added later.
 <br>
 <br>
 <i>Administator - Edmachine</i>
 </div>
 
 </div>
 </body>
 </html>";
 
 }
  ?>

I logged in, refreshed the index.php page, but now, I don't see the header! The <?php include(header.php); ?> doesn't work... but if I take out the session thing (nothing happens to index.php if you log in), the header shows...

Edited by Edmachine, 17 July 2007 - 05:15 AM.


#10 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 17 July 2007 - 06:27 AM

lol, no idea why ur echoing everything

replace

 <?php include('header.php'); ?>

with

".include('header.php')."

as your echoing php within php....just not gonna happen is it lol

Edited by curthard89, 17 July 2007 - 07:08 AM.


#11 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 July 2007 - 11:27 AM

But... you're now going to have an unnecessary '1' randomly right before the header, because the include construct returns a value.

Do this instead, and just simply exit the PHP.

I also fixed all of the repeating code.

And you definitely don't have a well structured HTML. ID should be unique; for what you're doing with the ID 'main', you should make it a class.

<?php session_start(); ?>
<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
a:hover{background:#666;}
a{text-decoration:none;}
</style>
</head>
<body link="#000" alink="#666" vlink="#666">
<?php include('header.php'); ?>
<div id="wrapper">
<div id="head">Welcome to Techgeeks!</div>
<div class="main">
<?php
if(isset($_SESSION['s_username']))
  echo "Hello, You are logged in as {$_SESSION['s_username']}. <a href=\"member.php\">Click here</a> to go to your Control Panel.';
else
  echo 'If you have an account, please click <a href="login.php">here to log in</a>. Administrators log in <a href="adminlogin.php">here</a>.<br />If you don't have an account, please take time to <a href="register.php">register here</a>. It is completely free!';
?>
</div>
<br />
<div class="main">Hello, this is this site's owner <b>Edmachine</b> speaking.<br />
<br />
The site is currently under construction, but you can visit a few places.<br />
I have planed to make the following:<br />
<del>A news system</del><br />
Personal Messenger for registered members<br />
A working search engine<br />
Member profiles<br />
<br />
More to be added later.
<br />
<br />
<i>Administator - Edmachine</i>
</div>

</div>
</body>
</html>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users