I need some links (related to user account) to appear on the index page for the user who logged in. i have a session variable'email'.
i did this but it didn't work.
<div id="left">
left div content
</div>
<div id=-"right">
<?php
if(isset($_SESSION['email']))
{
?>
//show user some links to his account.
<?php
}
else
{
?>
//show login and register forms
<?php
}
?>
</div>
<?php
session_start(); // add this line
if(isset($_SESSION['email']))
{
?>
Link to php manual.
your first statement within the
<?php
session_start();
//followed by rest of the code.
?>
should be
session_start();
Then the further code.
Related
I have a problem with PHP. When user is logged in, or session is started,
I want to hide div class, which is button "Login".
How can I make this with PHP?
Instead of trying to hide it, never output the div if the session has login information.
I.e. in your template / code that's outputting the HTML, check if the session is present (you'll have to swap user with whatever key you're storing the valid login under):
<?php
if (empty($_SESSION['user'])):
?>
<div class="login"> ... </div>
<?php
endif;
?>
.. and if you need it in a function:
<?php
function show_login_if_unknown() {
if (empty($_SESSION['user'])) {
echo '<div class="login"> ... </div>'; // or use ?> ... <?php
}
}
This is my php code
<?php
require_once('../includes/config.php');
// include file to check, If current user loggedin or not
include('log-security.php');
require_once('../cs/includes/header.php');
if(in_array(cms_username, $allowedUsers))
{
$allowedUsers=['john', 'akhil'];
<div id="view8"></div>
}
?>
This is my div id code
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
Im new to PHP, Please help me to correct the code if its wrong .
while login only it should restrict the user.when allowed user is entered then the viewid should displayed for others it should not.
Try something like that :
<?php
require_once('../includes/config.php');
require_once('../cs/includes/header.php');
include('log-security.php');
$allowedUsers = array('john', 'akhil');
?>
<?php if (in_array('cms_username', $allowedUsers)): ?>
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
<?php endif; ?>
I'm developing a php application and i have a problem retaining session values. I have two files, one is a sidebar (sidebar.php) and a home page (home.php). I have included the sidebar on the home page.
There are login controls on the sidebar and i can successfully login. I know it has successfully logged in because it shows me a message Welcome 'username'. But when i go to the home page, the welcome 'username' part is not shown as the session values are destroyed. instead the login form is shown. Why is that?
This is the home page (sidebar.php)
<?php require_once('connections.php'); ?>
<?php
// if the login button is clicked
if (isset($_POST['btnLogin']))
{
$myusername=$_POST['textusername'];
$mypassword=$_POST['textpassword'];
$result=mysql_query("SELECT * FROM users_table WHERE username='$myusername' and password='$mypassword'");
$count=mysql_num_rows($result);
if($count>=1)
{
$_SESSION['username'] = $row["username"] ;
$_SESSION['userid']= $row["ID"];
}
else
{
//Any code here
}
}
?>
<div class="col-md-12 right-aside">
<?php
if (isset($_SESSION['userid']))
{
echo Welcome : " . $_SESSION['username'];
echo " <a href='logout.php'> | Logout</a>";
}
else //if session is not set
{
echo 'Some html for login form';
}
?>
</div>
The following is the home page (home.php)
<?php include("head.php"); ?>
<body>
<div class="container container-body">
<div class="row">
<div class="col-md-9 main-content">
<div class="row">
<p>Some Text Here</p>
</div><!-- /.row -->
</div><!-- /.main-content -->
<div class="col-md-3">
<div class="row">
<?php include 'sidebar.php';?>
</div>
</div>
Are you starting a session in home.php?
You must call session_start() at the first line of every php script in which you want to access session variables.
Try adding this in top of home.
<?php
session_start();
//Then do your work
Then even if session_start is called again in head.php or sidebar.php , it will be ignored, as the session was already started.
<?php
session_start();
if(isset($_SESSION['login']))
{
include_once('includes/header.php'); ?>
<!DOCTYPE html>
<html>
<body>
<div id="mainframe">
<img src="img/header.png">
<div id="menu">
<?php include_once('includes/navbar.php'); ?>
</div>
<div id="content">
<h3>Shopping Cart</h3>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
else
{
header('location: login.php');
}
?>
Here is my small PhP code I've got at the moment, my login session is $_SESSION['login'].
And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out.
Does anyone know a simple method / solution for this?
Put this somewhere in your if statement.
It will show Logged in as User at right top corner of page
<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
U need to pass username using SESSION variable for the same
write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.
$query = "SELECT name FROM users WHERE password='$password'";
$username = mysql_result(mysql_query($query),0);
$_SESSION['username'] = $username;
than proceed as you are doing
<?php
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}
I have an admin link with a div id "admin". Sessions are started when a user is logged in to show if it is a normal user or an admin. Normal users can't access the files for admin, but can still see the admin link.
Is there a way to make it so normal users can't see the link, using only php or html, without jquery or jscript or any of those.
Using interleaved PHP & HTML with standard PHP syntax:
<?php
if ($user_is_an_admin) {
?>
<div id='admin'>
Only admins can see this...
</div>
<?php
}
?>
Alternate templating syntax:
<?php if ($user_is_an_admin): ?>
<div id='admin'>
Only admins can see this...
</div>
<?php endif; ?>
Not interleaving, PHP only:
if ($user_is_an_admin) {
echo "<div id='admin'>
Only admins can see this...
</div>
";
}
You'll need to use conditionals inside of your views:
<?php if($_SESSION['adminid'] == 1234): ?>
<!-- Admin div goes here -->
<?php else: ?>
<!-- Admin link goes here -->
<?php endif; ?>