PHP session and hide table with jQuery - php

Is it possible to hide the table with jQuery if their is session present?
this is my session code
<?php
$username = $this->session->userdata('username');
if($username == TRUE){
echo "WELCOME ".$username;
}else{
echo "<div class='msg'>Please Log In</div>";
}
?>
and in my jQuery I dont know what to put in IF statement so I put it like this
if(".msg:visible"){
$('table').hide();
}
If possible I want to hide the table using jQuery

You can do using jQuery try below code
if ($(".msg").length > 0) {
$('table').hide();
}

if($(".msg").is(":visible"))
$('table').hide();

If you need to hide the table when session exists (when msg is not visible), you can try this with jQuery:
if (!$('.msg').is(':visible')) {
$('table').hide();
}
However, if you page reloads when your users log in, you could do it directly on the table without jQuery:
<table <?php echo $username == TRUE ? 'style="display: none"' : ''; ?>>
<!-- contents -->
</table>

Related

Display a link only if user is an admin

I am very new to PHP and I am trying to make a registration form only for an admin(no need for other users). I want to show one of the menu nav ("Add photo") only to admins.
LOGIN.php:
<?php
include_once 'header.php';
$username = "Efren";
$password = "111";
if (($_POST['txt_uname_email'] == $username)&& ($_POST['txt_password'] == $password)) {
session_start();
$_SESSION['admin_is_logged'] = true;
echo '<script type="text/javascript"> window.open("homepage.php","_self");</script>';
}
This is the part of the header that I am trying to show only to admins:
<?php
if (isset($_SESSION['admin_is_logged']) && $_SESSION['admin_is_logged'] == true){
echo '<li>add photo</li>';
}
?>
</ul>
Right now “add photo” is hidden both to admin and other visitors.
You need to start session on every page you want access to $_SESSION variable. I saw your session_start is inside if statement. Just set it on top of every file (where you need session) and it should work.
Put
session_start();
on file beginning just after <?php

PHP - Hide/Show Content based on User Level

I have login page and user page, which after user login with correct username and password, it will go to user page.
The user page consists of a table of user list and have a 'Edit User' button next to each user row. For example, in the screenshot here -->
http://i.stack.imgur.com/K3qxi.png
So, based on user level, if user level = User (which has ID=4), i want to hide the edit button. What I've tried so far is like this but its not working. I have imported the session code and all the queries. This code below is just some part of what I want to do.
include_once("session.php");
if($_SESSION['userlvl']==4){
echo "USER";
?>
<script>
$document.(ready(function){
$(#editbutton).hide();
});
</script>
<?php
}
?>
<a id="editbutton"><img src="Edit.png"/></a>
you can use class instead of Id
include_once("session.php");
if($_SESSION['userlvl']==4){
echo "USER";
?>
<script>
$document.(ready(function){
$(".editbutton").hide();
});
</script>
<?php
}
?>
<a class="editbutton"><img src="Edit.png"/></a>
As was pointed out by #insertusernamehere you should use PHP to control what is written to the page depending upon the user's access level - merely hiding content from view is just asking for trouble as it is trivial to find when looking in code view. Below is just an example of how you might do this.
<?php
include_once("session.php");
?>
<html>
<head>
<title>Restrict content based upon user level example</title>
<?php
if( isset( $_SESSION['userlvl'] ) ){
$level=intval( $_SESSION['userlvl'] );
/*
If you have javascript functions that actually submit the forms or
interact somehow with the data that are to be restricted to particular
levels of user, generate the code for that level only.
*/
switch( $level ){
case 1:
/* Basic user */
echo "
<script id='user' type='text/javascript'>
function view_record(){
alert('hello world');
}
</script>
";
break;
case 2:
/* Superuser */
break;
case 3:
case 4:
/* Manager & Admin level users ? */
echo "
<script id='admin' type='text/javascript'>
function delete_all_users(){
}
function drop_tables(){
}
function drop_database(){
}
function edit_record(e){
alert(e+' edit record')
}
function delete_record(e){
alert(e+' delete record')
}
</script>
";
break;
}
}
?>
</head>
<body>
<table>
<?php
/* Assuming you use php to generate the table: pseudo style code */
while( $rs=$result->fetch() ){
$editbttn='';
$deletebttn='';
if( in_array( $level, array( 3,4 ) ) ){
/* Admin & manager */
$editbttn="<a class='editbutton' onclick='edit_record(event)'><img src='Edit.png'/></a>";
$deletebttn="<a class='deletebutton' onclick='delete_record(event)'><img src='Delete.png'/></a>";
}
echo "
<tr>
<td>DATA</td>
<td>DATA</td>
<td>DATA</td>
<td>$editbttn</td>
<td>$deletebttn</td>
</tr>";
}
?>
</table>
</body>
</html>
I assumed that you want to restrict the user that only has the privilege of "User" not to be able to edit the data?
You might want to consider trying this as I think it is more straight forward and simple?
session_start();
if($_SESSION['userlvl'] == 4) {
echo "USER";
}else{
$userlvlpermission = '<a id="editbutton"><img src="Edit.png"></a>';
}
//At where you wanted to place the "editbutton"
<?php if($userlvlpermission){echo $userlvlpermission;}?>
Be sure that you check the user session of the edit page as well in case they know the exact url.
AT edit page
<?php
session_start();
if ($_SESSION['userlvl'] = 4){
echo "NO PERMISSION";
echo "header('Refresh: 3;url=page.php')";
exit();
}
Here's a simple solution.
include_once("session.php");
if ($_SESSION['userlvl'] == 4) {
$hideme = 'display:none;'
} else {
$hideme = '';
} ?>
< a id = "editbutton"
style = "<?php echo $hideme; ?>" > < img src = "Edit.png" / > < /a>

Trouble returning session data (user name)

I'm trying to integrate a php login script that I have working, but I can't seem to get simple php calls going on a page. On this user profile page, I want to simply have the user name displayed (mysql field is "name"). The user is logged in and the session carries through, but on this page, all I see is the text "Here is your profile info..." What might be wrong in the code to prevent the user name from displaying?
<?php
include_once('classes/check.class.php');
include_once('header.php');
if( protectThis("*") ):
if(!isset($_SESSION)) {
session_start();
}
if(isset($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
?>
<br />
Here are is your profile info...
<?php
else :
?>
<div class="alert alert-warning">
<?php _e('Only signed in users can view what\'s hidden here!'); ?></div>
<?php
endif;
include_once('footer.php');
?>
For check session is set already use session_id() Also check you have set $_SESSION['jigowatt']['name'] already with empty()
if(session_id() == '') {
session_start();
}
if(!empty($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
else {
echo 'username is empty';
}
You need to put session_start(); at the very top of the page. No white space can be put before that. Try if that works.
First you need to write the sessions at the very top of the page if it works than okay else you can try this.
Just append this 2 function before and after the session_start();
Like this
ob_start();
session_start();
ob_end_clean();

Make a div visible from an outside php

I'm working on a log in session, and I want to display errors on the same page, for example - "Invalid Password" or "User does not exist".
Heres my code:
<?php
session_start();
mysql_connect('mysql.database.com','user','database')or die ('Connection Failed: '.mysql_error());
mysql_select_db('database')or die ('Error when selecting Database: '.mysql_error());
function remove($mensaje)
{
$nopermitidos = array("'",'\\','<','>',"\"");
$mensaje = str_replace($nopermitidos, "", $mensaje);
return $mensaje;
}
if(trim($_POST["usuario"]) != "" && trim($_POST["password"]) != "")
{
$usuario = strtolower(htmlentities($_POST["usuario"], ENT_QUOTES));
$password = $_POST["password"];
$result = mysql_query('SELECT password, usuario FROM usuarios WHERE usuario=\''.$usuario.'\'');
if($row = mysql_fetch_array($result)){
if($row["password"] == $password){
$_SESSION["k_username"] = $row['usuario'];
header( 'Location: diseno.php' ) ;
}else{
echo '<p class="message2">Invalid password</p>';
}
}else{
echo '<p class="message2"User does not exist</p>';
}
mysql_free_result($result);
}else{
echo '<p class="message2">Must enter a user and password</p>';
}
mysql_close();
?>
<SCRIPT LANGUAGE="javascript">
location.href = "index.php";
</SCRIPT>
As you can see that's my validation and action for the log in form. Instead of echoing errors in a new page I want to display it in the same page. I tried with javascript and it didn't work I used.
var page = document.URL = "http://www.mysite.com/login.php"
page.getElementById( 'wrongpassword' ).style.display = 'block';
All divs I want to display are set as none in the login.php file.
Anyone could help me?
The easiest way to accomplish this is to process the login and then include the PHP code which displays the normal page. I'm not sure how you've designed your site, but including index.php at the end might do the trick. Right now, you are using a JS redirect, which won't give you the result that you want.
Instead of echoing the message, I like to set a $message variable which includes the message. When you render the main page, simply echo this variable in the appropriate place if it is set.
For doing it simply you can make use of JQuery. I have done it on my website so I can say it really works.
Start your session, checking the values and either assign the value in global variables of javascript or print it there only
eg.
<?php
session_start();
//checking ur values
echo "<script src=\"js/jquery-1.8.3.min.js\"></script>
<script type=\"text/javascript\">
$(document).ready(function(){
//you can assign values here or print error messages to any div
('.div_class').html("unuthorised user");
});
</script>";
?>
Here I have used a downloaded JQuery file from
http://jquery.com/download/
You can choose other wise to use the online version of this JQuery file. The syntax for that is
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
Feel free to get back in case of any further query/issues regarding the above code.

How to create a pop up window using php via echo?

Is it possible to make a pop up window in my existing script?
session_start();
$_SESSION['success'] = ($result) ? TRUE : FALSE;
header('location: inv_fc.php');
session_start();
if ($_SESSION['success'] == TRUE) {
// CREATE POP UP WINDOW SUCCESS
} else {
// CREATE POP UP WINDOW FAILURE
}
You can do it with Javascript. For nicer results, use jQuery UI.
if ($_SESSION['success'] == TRUE) {
echo "<script>alert('Success!');</script>";
} else {
echo "<script>alert('Failure.');</script>";
}
You could open a pop-up using javascript or target a's attribute, but it's impossible from PHP, which is executed at server side.
Edit: ok, as I saw the <script> things: it's not PHP, it's Javascript, from PHP it's not possible.
<?php if ($_SESSION['success'] == TRUE)?>
<script>window.open(...);alert('Your Awesome!');</script>
<?php else ?>
<script>window.open(...);alert('You Fail!!');</script>
<?php endif; ?>

Categories