How do I prevent users from removing display:none in source code? - php

With some if/else PHP statement, I can show or hide some divs depending on if the profile page being checked out by a user belongs to that user or not.
For instance,
$id = $_SESSION['id'];
if ($uid == $id)
{
?>
<div id="block_user"></div>
<?php }
else { ?>
<div id="block_user" style="display:none"></div>
<?php
} ?>
The issue is even if the profile I'm checking out doesn't belong to me, if I inspect the element with Google webmaster tool, and remove
style="display:none"
the div becomes visible again.
How could I prevent that?

just remove the else part
<?php
if ($uid == $id) {
?>
<div id="block_user"></div>
<?php } ?>

If you want to hide stuff for real you have to dynamically create it within the PHP code... Such as :
<php
if ($uid == $id){
echo '<div id="block_user"></div>';
}
?>
which I guess you already are doing but simply dont print it out if you dont want to show it.

instead of hiding the div, remove the div element which you don't want to show.
$id = $_SESSION['id'];
if ($uid == $id)
{
?>
<div id="block_user"></div>
<?php } ?>

Related

PHP ... display array (results of survey) using forloop [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I am new to PHP and working on an assignment. I'm almost finished but feel like I'm beating my head against the wall with this last step.
The user chooses a survey and then answers 10 questions. After the 10th question is answered, they are redirected to the results.php page which should display their questions and answers.
I know that I need to use a foreach loop but this is my first time working with them. I've finished multiple sites to get an understanding of how they work but the examples are not helping me to understand how to make it work in my assignment.
Right now I get this when I run the file:
Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH), expecting end of file in C:\xampp\htdocs\sandbox\HW4\results.php on line 38
When I remove the endforeach, I'm told that $answer within the echo is undefined.
I've run out of ways to change my loop. Nothing works. Some help would be appreciated. I apologize if I include code that is not relevant to my problem.
With little understanding of PHP, I'm not quite sure what I need to include and what I don't. I'm including code from my survey.php file that includes the array session and my code from the results file that should contain my foreach loop.
Thank you for your time.
$isPostBack = filter_input(INPUT_POST, 'submitButton') !== NULL;
if ($isPostBack) {
// This is what happens if there is a postback.
$choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_INT);
if ($choose_survey !== NULL) {
// Check the value of $choose_survey and then set 'survey' accordingly, e.g.
// These numbers are the keys to the arrays with the list of surveys
switch ($choose_survey) {
case 0:
$_SESSION['survey'] = $survey0;
break;
case 1:
$_SESSION['survey'] = $survey1;
break;
case 2:
$_SESSION['survey'] = $survey2;
break;
default:
break;
}
$_SESSION['answers'] = array();
$_SESSION['number'] = 1;
} else {
// A survey is not selected because it was already chosen.
// get the value from the radio button.
$answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
if ($answer == NULL) {
echo '<p>Please select an answer before clicking Submit.</p>';
} else {
$question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_INT);
$question = $_SESSION['survey'][$question_key];
// this will be used later to display the answers/results
$_SESSION['answers'][$question] = $answer;
unset($_SESSION['survey'][$question_key]);
// This is adding 1 to the question number unless session number is 10
if ($_SESSION['number'] == 10) { // if = to 10 then we redirect to next page.
header('Location: results.php');
} else { // If number is less than 10, we add 1
$_SESSION['number'] += 1;
}
}
}
} else {
// This is what happens if there is no postback.
}
?>
results.php file
<br /> <p>Thank you for participating in the survey!</p>
<html lang="en">
<head>
<title>Survey Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="logout.php">
<p>Here are your results</p>
<input type="submit" id="submit" value="Logout" />
</form>
<section>
<?php foreach ($_SESSION['answers'] as $question => $answer) ?>
<p>
<?php echo "$answer <br/>"; ?>
</p>
<?php endforeach; ?>
</section>
</body>
did you loaded the session_start() on your file? If not this may not enable to save your sessions that you are setting. Also on your results.php you can change your code to:
<?php foreach($_SESSION['answers'] as $question => $answer){ ?>
<p>
<?php echo $answer."<br/>"; ?>
</p>
<?php }
?>
Hope this helps!
I am not sure, but maybe you could place <?php session_start();?> at the beginning of your results.php to make your script able to work with $_SESSION variable?
Also try to var_dump($_SESSION); as in result.php, just in case.
I surprised that nobody noticed the syntax error in the foreach.
If you want to use the endforeach style, you can change your code like this (this is called Alternative Syntax):
<section>
<?php foreach ($_SESSION['answers'] as $question => $answer): ?>
<p>
<?php echo "$answer <br/>"; ?>
</p>
<?php endforeach; ?>
</section>
Or if you want to use PHP-way to make a foreach, you can write it like this:
<section>
<?php foreach ($_SESSION['answers'] as $question => $answer) { ?>
<p>
<?php echo "$answer <br/>"; ?>
</p>
<?php } ?>
</section>
However, for cleaner HTML & PHP code, I suggest you to write it like this:
<section>
<?php
foreach ($_SESSION['answers'] as $answer) {
echo "<p>$answer</p>" . PHP_EOL;
}
?>
</section>
Note that I removed unused $question variable.

PHP - Logical operator with IF/Else statement test not working correctly

Code from: oc-content/themes/realestate/item.php
I have this piece of PHP code which is part of a 'framework' called OSClass. Basically, I am trying to 'hide' the user (the publisher of the advert) name to non-logged in users. In other words, only if a user is logged in, only then they can see if the publisher users advert.
I found out that I need to add this piece of code osc_is_web_user_logged_in(), which has worked perfectly for another section, however, due to the else statement, the publishers name is still displaying...
How can I amend the else statement? I could remove the else statement, but I'm worried it will 'break' something and I am unsure what osc_item_user_id() does... Do I add another if statement within the else statement (complete PHP newbie here).
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } else { ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
Thanks!
In my opinion you should simple write:
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
As you don't want to display publisher name when user is not logged, you don't need have else section.
You can add an else if statement.
if (osc_item_user_id() != null && osc_is_web_user_logged_in())
{
<?php echo osc_item_contact_name(); ?>
}
else if (user non logged)
{
// your stuff
}
else
{
<?php echo osc_item_contact_name(); ?>
}

PHP foreach help

I am building an extention to my site that allows users to favourite posts, I am however having a a problem,
I am running this code in my view,
<?php if(isset($favourites)) : ?>
<?php foreach ($favourites as $fav) : ?>
Fave
<?php endforeach;?>
<?php else : ?>
Fave
<?php endif; ?>
However my problem is that if there are two posts marked as a favourite it will loop over both entries and show the favourite link twice on one post, how can I make it so it loops through the posts and adds a favourite link if that post is indeed a favourite?
If I understand your question:
if (isset($favourites)) {
foreach ($favourites as $fav) {
// i am a favorite
}
} else {
// i am not a favorite
}
If you have the list of links already, which it looks like you do with $j:
foreach ($j as $jj) {
if (isset($favorites[$j])) {
// i am a favourite
} else {
// i am not a favourite
}
}
done.

Show/Hide Div Based on URL using PHP

I want to show a DIV ONLY when on /../mahjong.php. So even if I go to /../mahjong.php?layout it should hide the div (since it's not the same url)
I have tried the following:
// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php") >= 0) {
$style = "display: none";
}
else {
$style = "display: inline";
}
And my div ofcourse:
<div class="menu" id="menu" style="<?php echo $style; ?>">
But if I go to /games/mahjong/mahjong.php?layout it doesn't change the style. I've echoed:
echo $_SERVER['REQUEST_URI'];
and it changes to /games/mahjong/mahjong.php?layout, so why isn't the style set to inline?
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php") === false) {
Didn't work either. (this wil show the div and never hide it) What am I missing?
Many thanks,
Maurice
Check if the $_GET array has been populated or not:
<?php if (empty($_GET)): ?>
<div>
...
</div>
<?php endif; ?>
Should be sufficient if you're not manually adding to the $_GET array, which would be very silly.
Change your condition to:
strrpos($_SERVER['REQUEST_URI'], '/games/mahjong/mahjong.php') === strlen($_SERVER['REQUEST_URI']) - strlen('/games/mahjong/mahjong.php')
This will make sure the request uri ends with that string.
strpos(); only search for that string, and in both cases, string is found
strpos(); isn'T exact search !
// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php?layout") >= 0) {
$style = "display: inline";
}
else {
$style = "display: none";
}
or you can use
// We're NOT on the home page
if (isset($_GET['layout'])) {
$style = "display: inline";
}
else {
$style = "display: none";
}
this might help
Check whether PATH_INFO AND QUERY_STRING are empty...if not, then it's not the page you want.

Keeping track of language in a session variable

What my current code does is, while checking the DB if the versions (FR and EN) are either True or False, display the proper content and if both exist to display a link so that users can switch languages. If only one language exists, the content is shown in that language and there is no link displayed.
the 2 functions in javascript are like this, here`s the FR one:
function makeVisibleFR()
{
document.getElementById('bbqc_contentFR').style.display="inline";
document.getElementById('bbqc_contentEN').style.display='none';
document.getElementById('vFrancais').style.display='none';
document.getElementById('vAnglais').style.display="inline";
}
What i`d like to add to this is the option of memorizing the user's choice and displaying the following pages with the same language version.
I imagine i'd need to create a $_SESSION['language'] variable and store in it either "FR" or "EN" but i`m not sure how to go about implementing that within my current code.
<?php
if($versionFR == true)
{
if($versionEN == true)
{
?>
Version Anglaise
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
Version Française
<div style="display:none" id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
<?php
}
}
else
{
if($versionEN == true)
{
?>
<div id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<h2>Erreur, il n`y a aucun texte</h2>
<?php
}
}
?>
Here's a simplistic example:
// assuming that databaseHas() queries available languages
session_start();
$langs = array('ENG', 'FR');
$showlang = '';
if (databaseHas($_SESSION['lang']))
{
$showlang = $_SESSION['lang'];
}
else
{
foreach ($langs as $l)
{
if (databaseHas($l))
{
$showlang = $l;
break;
}
}
}
if ($showlang == '')
{
die('No languages found!');
}
echo databaseContent($showlang);
// print links to alternate languages
foreach ($langs as $l)
{
if (databaseHas($l) && $l != $showlang)
{
// print link to this language
}
}
put session_start() at the top of your file then:
if($versionFR == true) $_SESSION['lang'] = 'FR';
else $_SESSION['lang'] = 'ENG';
//later on (could be in a whole other page with session_start() on top)
if($_SESSION['lang'] == 'FR'){/*display FR stuff*/}
elseif($_SESSION['lang'] == 'ENG'){/*display ENG stuff*/}
Something like that should work well for ya ^_^
How would i go about adding that if 1)
both versions exist 2) lang is ENG 3)
english part is display:none by
default
1) You'd just add the clause: if($_SESSION['lang'] == 'FR' && $_SESSION['lang'] == 'ENG')
2) Not sure what you mean here, Neal explained it well from what I can see
3) If ENG is display:none by default, you'd want to fire off a javascript function to toggle it back on.
But let's take a step back here, consider this: Make two language files that define each piece of content. So for your english.php you might have variables such as $GREETING = 'Hello'; $YES = 'YES'; and then in your french.php you'd define these variables as $GREETING = 'Bonjour'; $YES = 'WEE'; (I'm not even sure if wee means yes, but you get the idea!). So now you can choose to include the appropriate language file based on the user's language, and you make it easy to add another language down the road. Be flexible!

Categories