Reputation: 5
I am truly a novice at coding and only succeed with trial and error. I use a WYSIWYG program to do all the main pages in my site and then add php coding to do some specified things.What I am trying to do right now is display a log in button along with a register and forgot password links to those forms, all of which I have built and working, have this display in the masterframe page when a user is not logged in and show another set of user name, profile, logout links when they are logged in. By themselves I have all these functions working, I just cant figure out how to do it this way. Any help or steering me in the right direction to teach me would be great. If you need to be paid for your help that can be arranged as well. Thank You.
update: This is the code that I have right now and use, again I want to have the if else statement show one thing or the other on condition, and have it show in place of, all on the masterframes page.
// have this display if user is logged in
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
}
else
{
echo 'Not logged in';
}
?></span>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Edit_Profile.php',{width:620,height:710})" target="_self" class="style5">Profile</a></span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
//have this display if user is logged out
<a href="javascript:displaylightbox('./members/Log-In.php',{width:490,height:370})" target="_self" id="jQueryButton1" style="width:100%;height:100%;">Log In</a>
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Create_Account.php',{width:620,height:710})" target="_self" class="style5">Register</a></span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Forgot_Password.php',{width:350,height:275})" target="_self" class="style5">Forgot Password?</a></span>
</div>
I have tried doing this but I keep getting a syntax error for unexpected '<'
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Edit_Profile.php',{width:620,height:710})" target="_self" class="style5">Profile</a></span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
}
else
{
<a href="javascript:displaylightbox('./members/Log-In.php',{width:490,height:370})" target="_self" id="jQueryButton1" style="width:100%;height:100%;">Log In</a>
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Create_Account.php',{width:620,height:710})" target="_self" class="style5">Register</a></span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Forgot_Password.php',{width:350,height:275})" target="_self" class="style5">Forgot Password?</a></span>
</div>
}
?></span>
Upvotes: 0
Views: 93
Reputation: 149
Create a PHP session and use that session variable for your "IF" condition boolean.
i.e. if (session active) {then display this object} else {dont display this object}
Here is some documentation on PHP 5 Sessions. PHP 5 Sessions
The neat thing about PHP is that it is completely interchangeable with HTML. Therefore you and assign elements to divs. Here is an example.
<html>
<body>
<?php
$loggedInTxt="";
$loggedOutTxt="";
if (*session*){
$loggedInTxt="<div>*some html content here*</div>"
}
else{
$loggedOutTxt="<div>*some html content here*</div>"
}
?>
<?php echo($loggedInTxt)?>
<?php echo($loggedOutTxt)?>
</body>
</html>
The idea is that you test the condition within the php and create php strings containing html elements. You can insert the anywhere in your html. So if you create a button and assign the code you used to create that button to a php variable then you can echo that variable(the code for the button) anywhere in your html script. I was blown away by the implications of php. I hope this helps simplify it! (This more of an overview. Do NOT copy and paste the code)
Upvotes: 0
Reputation: 520
You are mixing both PHP and HTML code. Please correct.
You have to separate / embed HTML properly in your document while using php conditions.
Example:
<?php
$condition = true;
if ($condition) {
?>
<h1>This will be displayed when condition is true</h1>
<?php
} else {
?>
<h1>This will be displayed when condition is false</h1>
<?php
} // else ends
?>
Please try this:
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
?>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Edit_Profile.php',{width:620,height:710})" target="_self" class="style5">Profile</a></span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
<?php
}
else
{
?>
<a href="javascript:displaylightbox('./members/Log-In.php',{width:490,height:370})" target="_self" id="jQueryButton1" style="width:100%;height:100%;">Log In</a>
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Create_Account.php',{width:620,height:710})" target="_self" class="style5">Register</a></span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;"><a href="javascript:displaylightbox('./members/Forgot_Password.php',{width:350,height:275})" target="_self" class="style5">Forgot Password?</a></span>
</div>
<?php
}
?></span>
Upvotes: 0
Reputation: 187
In order to do what you are trying, you need to implement just a bit of logic on the code. The example of kerv is perfectly valid. The idea is that you will validate if the user is logged in or not, before rendering the html. For example:
if($userLoggedIn){
<div> Welcome to the site </div>
} else {
<div> Your are not logged in, please do so to continue </div>
}
I'll suggest you to edit the question with some code so we can properly help you.
Upvotes: 0
Reputation: 520
<?php
// Setting a session variable when customer is logged in.
$_SESSION['user_loggedin'] = 1;
$_SESSION['customer_id'] = $customer_id; // Some reference of logged in customer
$_SESSION['customer_name'] = $customer_name; // Customer information collected from DB or other resource.
// Deciding whether to display "Login" button or Logged in status / links
if ($_SESSION['user_loggedin']) {
echo 'Hi ' . $_SESSION['customer_name'];
echo '<a href="/myaccount">My Account</a>';
} else {
echo '<a href="/login">Login</a>';
echo ' <a href="/register">Register</a>';
echo ' <a href="/forgotpassword">Forgot Password</a>';
}
If you have some PHP function to check whether customer is logged in or not, you can use that function like this in lieu of if ($_SESSION['user_loggedin'])
condition
if (UserLoggedin()) {
// Logged in links
} else {
// Links to be displayed when customer is logged out.
}
Upvotes: 1
Reputation: 315
Adding some code in your question would be nice, but if I understand your question correctly you might want to try this:
$logged_in = 0;
if($logged_in == 0) {
//all the stuff you want to show when the person is not logged in
$logged_in = 1;
}
if($logged_in == 1) {
//all the stuff you want to show when the person is logged in
}
Upvotes: 0