How can Add logout script

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 6572 Views - Last Post: 18 February 2011 - 03:22 PM Rate Topic: -----

#1 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

How can Add logout script

Posted 16 February 2011 - 06:44 AM

Hi all,

I'm trying to add logout script and also when users login to display their name. But when the user login it bring to tab.php but it doesn't display the user has login or the logout link. How can I link it all together. This what I've so far. Thank in advance.

login.php

<?php
	ob_start();
	$conn = mysql_connect("localhost","root", "");
	mysql_select_db("mycontinent", $conn);

	//IF SUBMIT BUTTON PRESSED
	if(isset($_POST['signin'])) 
	{
	   	if(!$_POST['username']) die("Error: You must enter your email before logging in.");
	   	if(!$_POST['password']) die("Error: You must enter your password before logging in.");
	    
	   	$username = $_POST['username'];
		$password = md5($_POST['password']);
			
		$sql = "SELECT * FROM `login1` WHERE username = '$username' AND user_password = '$password'";
		$result = mysql_query($sql, $conn);
		$q = mysql_fetch_array($result);
		    if(!$q)
		    {
			echo <<< form
			<form action="indexAfrica.php" method="post">
			<button type="submit"  style="background-color:blue;">
			<p style="font-size:120%" style="color:white">Click to try again</p></button>
			</form>

form;
			die("Login Failure: An error occured, please verify your email and password are correct.");
			}
		    else 
		    
		    $_SESSION[logged_in]=1;
		 		
	header("Location: tab.php");
	}
	
	else
	{

.
.
.<table border="1" align="center" bgcolor="#00CCFF" width="500" heigth="" cellpadding="10">
<td>
<table border="0" align="center" bgcolor="">
	<form name="signup" action="loginForm.php" method="POST">
	<br>
		<tr>
		     <td><p style="color:white"><big><b>Username:</td>
		     <td><input type="text" id ="username" name="username" value="" maxlength="30"></td>
		</tr>
		
		<tr>
		     <td><br><p style="color:white"><big><b>Password:</td>
		     <td><br><input type="password" id="password" name="password" value="" maxlength="30"></td>
		</tr> 
		<tr>
		      
		      <td><br><input type="submit" id="Login" name="signin" value="Login"></td>
		</tr>
	</form>
	
</table>
?>


members.php

<?php 
session_start(); 
 
if (!$_SESSION["valid_user"]) 
        { 
        // User not logged in, redirect to login page 
        Header("Location: loginForm.php"); 
        } 
 
// Member only content 
// ... 
// ... 
// ... 
 
// Display Member information 
echo "<p>User ID: " . $_SESSION["valid_id"]; 
echo "<p>Username: " . $_SESSION["valid_user"]; 
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]); 
 
// Display logout link 
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";


logout.php

<?php 
session_start(); 
session_unset(); 
 
session_destroy(); 
// Logged out, return home. 
Header("Location: index.php"); 
?>


Is This A Good Question/Topic? 0
  • +

Replies To: How can Add logout script

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: How can Add logout script

Posted 16 February 2011 - 07:09 AM

line #20: <<<form (w/o space inbetween)
Was This Post Helpful? 0
  • +
  • -

#3 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 16 February 2011 - 07:42 AM

Thank for your reply but it didn't change anything.
Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: How can Add logout script

Posted 16 February 2011 - 07:54 AM

Before
session_destroy()
add

$_SESSION["valid_user"] = false;


this will make sure to clear the valid user session that is checked
Was This Post Helpful? 0
  • +
  • -

#5 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 16 February 2011 - 04:12 PM

Thanks, but it didn't change nothing. The problems is when user login then it takes user to tab.php that place that I want to display the name of user who loggin and logout link to give the user opportunity to logout. Check login.php then you'll see this:

$_SESSION[logged_in]=1;
		 		
header("Location: tab.php");


that bring the user to the tab.php, I want to display logout and user name in tab.php after user login. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#6 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: How can Add logout script

Posted 16 February 2011 - 04:17 PM

Are you putting session_start() at the top of the login? and tab.php?
Was This Post Helpful? 0
  • +
  • -

#7 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 16 February 2011 - 05:30 PM

if I add session_start() on top of loginform php. it showing this error message because of ob_start()
Parse error: parse error in C:\wamp\www\myproject1\loginForm.php on line 3

Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: How can Add logout script

Posted 16 February 2011 - 05:33 PM

No, a parse error means your syntax is off somewhere. Chances are you forgot a semicolon, or have an extra closing parentheses in there or something. Without seeing the code you've got now, I couldn't tell you exactly what is causing it, but it is definitely syntax related.
Was This Post Helpful? 1
  • +
  • -

#9 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 16 February 2011 - 05:48 PM

Ok. you're right.
I'm tryig to add session_start() on top of tab.php. this what I've done.

<?php
      session_start();
  ?>    
<html>
	<head>
		title>Tab</title>
         got css
        </head>
    <body>
    </body>
</html>

but still no showing the user who login in tab.php
Was This Post Helpful? 0
  • +
  • -

#10 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: How can Add logout script

Posted 16 February 2011 - 06:59 PM

missing a '<'

Should be, that will make the page title "Tab"
<title>Tab</title>


To show the $_SESSION["valid_user"] use it where you want it to display.
<body>
<?php echo($_SESSION["valid_user"]); ?>
</body>

Was This Post Helpful? 0
  • +
  • -

#11 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 17 February 2011 - 06:07 AM

This what I have done extra to display the username when user login. But it's not display the logout link. Thank in advance

there were problem with variables and added

loginform.php
 // Login good, create session variables 
        	$_SESSION["valid_id"] = $obj->id; 
        	$_SESSION["valid_user"] = $_POST["username"]; 
        	$_SESSION["valid_time"] = time();


also added this on

tab.php
<?php
	session_start();
	$valid_user = $_SESSION['valid_user'];
 
?>
<htm>
  <title> tab</title>
   <body>
       <?php 
		
		echo" you logged in as ".($_SESSION["valid_user"]);
		 
       ?>
   </body>

</htm>

Was This Post Helpful? 0
  • +
  • -

#12 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 17 February 2011 - 07:12 AM

I managed to display logout link on tab.php
when user click the link logout it takes user to index.php
added in tab.php
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";


Logout.php
<?php 
session_start(); 
session_unset(); 
 
session_destroy();
$_SESSION["valid_user"] = false; 
// Logged out, return home. 
Header("Location: index.php"); 
?>

But the problem is now.When user in index.php and click on arrow on top of browser still bring the user back to tab.php. How stop user from index.php to go back to tab.php even if user click on (<) arrow on browser to go back in previuos page. I tried to put session_start() on top of index pages but it didn't work. thank in advance
Was This Post Helpful? 0
  • +
  • -

#13 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: How can Add logout script

Posted 17 February 2011 - 07:17 AM

So you were wondering why it wasn't displaying a logout link when you didn't put the code in for it at all?
Was This Post Helpful? 0
  • +
  • -

#14 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How can Add logout script

Posted 17 February 2011 - 07:35 AM

Ok. thank for remind me.

I can't figure out now, how to stop the user from go back to tab.php when user used the arrow in browser when the user on index.php. Any suggestion! Thank in advance
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: How can Add logout script

Posted 17 February 2011 - 07:49 AM

you need to check in tab.php if the user is allowed to view the page, otherwise redirect back.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2