PDA

View Full Version : show/hide div tags PHP help


Gukkie
02-20-2010, 07:48 PM
Hi, hope some1 cud help me out,
i have a universal header file that sotres all of my div tags/boxes, the problem im having is because this is a universal header which is used in all the pages, i need to show n hide certain things if the user does certain things.

Right now, i want the div tag for the login form to be hidden when the user logs in successfully after validation, how do i go about doing this?

here is my code.

header.php
<?php
include_once "scripts/login.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style/main.css" rel="stylesheet" type="text/css" />
</head>
<body leftmargin="10" topmargin="10">
<center>
<table>
<tr>
<td>
<div id="header">
<div id="error">
<?php print $error; ?>
</div>
<div id="welcomebar">
<?php print $welcome?>
</div>

<div id="forgotpass"><a href="">Forgot Password?</a></div>

<div id="loginform">
<form action="" method="post" name="login">
Username:<input name="username" type="text" id="inputbox" maxlength="50" />
Password:<input name="password" type="password" id="inputbox" maxlength="15" />
<input type="submit" value="Login" name="cmdlogin"/>
</form>
</div>
</div>
</td>
</tr>
</table>
</center>
</body>
</html>

Login.php
<?php
session_start();

$username1 = "";
$password1 = "";

if ($_POST['cmdlogin']){

$username1 = $_POST['username'];
$password1 = $_POST['password'];

if ($username1&&$password1){
include_once "scripts/connect_to_mysql.php";

$username1=strip_tags($username1);
$password1=strip_tags($password1);
$username1=mysql_real_escape_string($username1);
$password1=mysql_real_escape_string($password1);
$username1=eregi_replace("`","",$username1);
$password1=eregi_replace("`","",$password1);

$password1=md5($password1);

$sql=mysql_query("SELECT * FROM myMembers WHERE username='$username1' AND password='$password1' AND email_activated='1'");
$login_check=mysql_num_rows($sql);

if ($login_check != 0){
while($row=mysql_fetch_array($sql)){

$id=$row["id"];
session_register('id');
$_SESSION['id']=$id;

$username1=$row["username"];
session_register('username');
$_SESSION['username']=$username1;

mysql_query("UPDATE myMembers SET last_log_date=now() WHERE id='$id'");
$error = 'Logged in.';
$welcome = 'Welcome, '.$_SESSION['username'];

}
}
else
$error .= 'No such record exists.';
}
else
$error = 'Please enter a username & password.';

} //close first if

?>

bocasz
02-21-2010, 11:22 AM
first off add $_SESSION['logged']=true; to the php code when the user logs successfully.

then in your header add
if(@$_SESSION['logged']){
$html = "what ever you want to show after loggin";
}else{
//prints the login form
$html="<div id=\"loginform\">
<form action=\"\" method=\"post\" name=\"login\">
Username:<input name=\"username\" type=\"text\" id=\"inputbox\" maxlength=\"50\" />
Password:<input name=\"password\" type=\"password\" id=\"inputbox\" maxlength=\"15\" />
<input type=\"submit\" value=\"Login\" name=\"cmdlogin\"/>
</form>
</div>"
}

Gukkie
02-21-2010, 11:58 AM
thanks for the help, it works flawlessly =)