PHP how to hide text from users who are not logged in? - php

Please tell me a way on how to make that if the user has not logged-in the site, he cannot see specific text, instead he will see something like "This content is only for registered users. To register click here." Or something around that way...
Thanks!

set SESSION['user_id'] value for the logged user
and u apply the logic on that specific div
if(!isset(SESSION['user_id']) || empty(SESSION['user_id']))
{
echo "Please <a href='register.php'>login/register</a> to view this content";
}
else
{
<div>
// your content and whatever
</div>
}

Pass your text from some conditions and check before echoing it. For an example
Check if the user is logged in though session variable directly or you can create method/function for this
if (is_user_logged_in() === TRUE)
{
echo $content;
}
else
{
echo "This content is only for registered users. To register click here."
}
This is just a simple example.

Related

How do I detect if page viewer is the current session

I'm trying to have a universal "Accounts" page that displays user profiles based on who is viewing it. If as a signed in user, this is my page, content served reflects my ownership. I tried something like
<title>
<?php
if ($_SESSION["first_name"]) { //this $_SESSION["first_name"] = $_COOKIE["first_name"]?
?> My Profile </title>
<?php
}
else
?>
User's Profile </title> <!--This line is wrong-->
But it does not do what I want it to. Also, that last line User Profile, does it mean I have to create a unique index page for each user? I thought PHP can dynamically make it possible to view a user's profile without doing a file write at each sign up?
Actually i cant understand ur question but its my guess that u need this .
$user_id = //user id from db of current viewing user
if($_SESSION['user_id'] == $user_id)
{
echo 'My profile';
}
else
{
echo 'user profile';
}

how to hide button for normal users and show for admin

I want to hide button in search menu to specific page for normal users so they can't access to page witch is for administrators only. i'm new at writing this code so i'm asking you guys who have much more knowledge than me for help.
I want to hide File/page name upload.php for normal users and show for administrators only
Is anyone know how could i do this with php?
i'm really appreciate for every help. Thank you!
You can capture user type of user's in session. And, according to user type show / hide button.
<?
if(S_SESSION['userType'] == 'Admin') {
//Show
}
if(S_SESSION['userType'] == 'User') {
//Hide
}
?>
It's hard to give an advice without your context, but an simple example for your scenario:
// Normal Page
if($_SESSION['user_level'] > x){
//show button
}
// Admin-Only-Page
if($_SESSION['user_level'] < x){
die("Access denied");
}
Depends of how you define the administrator, if he is the user whose the Id is 1 for example, use the following script:
<?php
$current_user_id = // get it from the session if user is logged in
If ($current_user_id == 1):
?>
<input ...>
<?php endif; ?>
For that you need to set one flag in Database as user is admin or normal user.
after that in your code check with condition that logged in user is admin or not;
for example admin role is 1 then:
if($user->role == 1){
// your button code
}
simple, Enjoy :)
for example, you can use following Conditional statement :
if ($user -> role == 1){
// `enter code here`
}

php-login choose page after login

I am using the minimal version of php-login from: http://www.php-login.net/ on Windows 7. When the user logs in, the login is verified and the response from the verification is True or False. The response is returned to index.php which has this code
if ($login->isUserLoggedIn() == true) {
include("views/logged_in.php");
I would like to give the user an opportunity to choose which page they go to after logging in but that page can only be accessible when the user is logged in. Any ideas? Let me know if you need more information or if what I am trying to do isn't clear. Thanks
======= UPDATED BELOW =========================
I modified the code below so that if the user login validation returns true, the user is directed to a page that has these two links Green Blue when the user clicks a link the user gets an Access Forbidden error message.
if ($login->isUserLoggedIn() == true) {
include("views/choose-pages.htm");
} else {
include("views/not_logged_in.php");
}
You can modify not_logged_in.php and have 2 submit buttons like Google, normal "Google search" and "I'm feeling luckey".
<input type="submit" name="login" value="Log in green" />
<input type="submit" name="login" value="Log in blue" />
then modify index.php
if ($login->isUserLoggedIn() == true) {
if ($_POST["login"] == "Log in green") {
include("green.php");
} else {
include("blue.php");
}
}
Well, try to create a main page (main.php), in this page write the code of choosing a page, its easy !!
if ($login->isUserLoggedIn() == true) {
include("views/main.php");
}
You could store your pages names/links in mysql database first and then get the pages links/names from the mysql database in an array and put it right after your code which would excute after the users are logged in. like so:
if ($login->isUserLoggedIn() == true) {
///Your array of pages here ///
EDIT:
Based on your comment, you can do this:
if ($login->isUserLoggedIn() == false) {
header(location someotherpage.php)
You could put that code in the blue.php, green.php etc etc
Second edit:
I hope i understand it correctly and if so, then you can use something like this:
if ($login->isUserLoggedIn() == false) {
echo "access denied";
you could even populate your page names and links in a drop down menu fashion so the users can select one and go to that page.

Enable/disable div according to guest/user entrance using php and jquery

I want to enable or disable a div according to the session if it starts with an user or a guest. I want to do something like this:
First, i will evaluate if it is user or not by doing this:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
?>
then in jquery, i would like to say:
$('.box').click(function(){ // labBox appears when box is clicked
if(<?php $guest?>)
$("#LabBox").hide();
else
$("#LabBox").show();
});
Question: how can i use my php boolean var $guest to disable or hide some elements of my website?
Do i have to do two distinct php files? one for users and other for guest (e.g, home.php and home_guest.php)?
you could do the alternative such as
<script>
var guest = '<?php echo $guest; ?>';
$('.box').click(function(){ // labBox appears when box is clicked
if(guest === "true") {
$("#LabBox").hide();
} else {
$("#LabBox").show();
}
});
</script>
This would simply allow you to pass the PHP value to a Javascript variable, in order for you to use it within the onClick.
Remember: everything that reaches the client can be manipulated. Therefore, if you send an hidden element (say, an hidden <div>) any tech-savvy user can, and will, easily make them visible.
You MUST perform the checks about the login/guest status in your PHP script, and don't rely on jQuery to assemble the page at client side (hey, after all, the user may have disabled javascript altogether!)
You don't need two pages (eg: home.php and home_guest.php) to render different content based on the user level. Just use appropriately session/cookies and different echos.
Use a hidden input, populated by PHP, which jQuery can grab:
<?php
echo "<input type=hidden id=guestcheck value=$guest/>"
?>
if ("#guestcheck").val()) {
}
I personally like this method because it allows me to check the source when debugging to find out where any errors may be (for instance you can plainly see in the source when viewing the page whether or not GUEST is true)
It depends on contents of those files. If the only difference is visibility of the block, it's more reasonable to do the check inline.
<?php if (isset($_SESSION['idUser'])) { ?>
$('.box').click(function() { $("#LabBox").show(); }
<?php } ?>
Personally I would do it in the HTML rather than the JS file...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$loggedin=true;
} else {
$loggedin=false;
}
?>
Then later on..
<?php if($loggedin===true){?>
<div>User is logged in</div>
<?php }else{?>
<div>Guest is viewing page</div>
<?php }?>
This means that the div for the user is not shown to the guest, whereas your currently solution only hides it from view (user could just use firebug/viewsource!
Why don't you just show/hide your div in the php depended on if they are a guest or not...
So...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
if($guest===true){
echo "<div></div>";
}
else{
//dont echo div
}
?>
PHP / server-side:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
// add #LabBox element from here to avoid junk/hidden elements for guests
}
?>
JQuery / client-side:
$('.box').click(function(){ // labBox appears when box is clicked
if (!<?php echo $guest?> && $('#LabBox').length > 0) {
$('#LabBox').show();
}
});
Then it is critical that any action requested by the user pass the "guest or not?" test before being granted from the server-side.

JQuery Allow Multiple Field Check Function

I have a simple option for users to check the "title" or "username" for availability. The function runs the ttck.php and displays the appropriate alert. Problem is the function() will not reset to allow multiple checks without reloading the entire page.
function checkTitle() {
$.post('ttck.php', $(".pubttl").serialize(), function(data)
{ $('.tresult').html(data).delay(5000).fadeOut('slow'); });
}
<span class="udo" onclick="checkTitle()"> check title availability</span>
What am I not seeing here to reload/reset this event so users can check as many times as necessary?
EDIT: This is the PHP
if(empty($TITLE)) { echo 'Please enter a Title';}
else { $que=mysql_query("SELECT * FROM `pubs` WHERE TITLE='$TITLE'");
$result=mysql_num_rows($que);
if($result>0) { echo 'Sorry, Title is already published. Try an alternate'; }
else { echo 'Huzzah, your Title is Available!'; }}
There are a few extended parameters in this statement, but none that would effect the function.
Did you try this
<span class="udo" onKeyUp="CheckTitle()"> check title availilibilty </span>
in your earlier script you were trying to run the function by using an Onclick function, whereas this one will run as the person is keying in the title. Hope this is what you want.

Categories