I'm trying to allow access for certain content only for specific member's user and administration.
With the following code I can't view the content when I'm logged as admin:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') || !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Also I tryed each condition individually, but as admin I can't view the content.
I tryed this example code:
<?php if (is_category(8)) {
if (!current_user_can('mio-gruppo') || !user_can('administrator')) {
echo 'you can not pass';
}
} else {
echo 'you pass';
}
?>
But the two conditional statement goes in conflict. If I try both separately works right
I guess it should be && instead of ||:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') && !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Alternatively make it the other way round:
<?php
if (is_category(4)) {
if (current_user_can('my-group') || current_user_can('add_users')) {
echo 'access granted';
} else {
echo 'no access';
}
}
?>
This is what I need:
<?php
if (is_category(8) && (current_user_can(array(1,2,3,4,5,6,7)) || !is_user_logged_in())) {
if (!current_user_can('mio-gruppo')) {
echo 'you can not pass';
}
} else {
echo ' you can pass';
}
?>
Related
Normally I use $_GET to catch the URL parameters and display content dependend on what the parameter is.
<?php
function isCmd($cmd) {
if(isset($_GET["cmd"]))
if($_GET["cmd"] == $cmd)
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the normal page</p>";
}
The url would look like these
example.com/script.php
example.com/script.php?cmd=page1
example.com/script.php?cmd=page2
example.com/script.php?cmd=page3
And it works as expected, but I see on websites they can do just:
example.com/script.php?page1
example.com/script.php?page2
example.com/script.php?page3
and it would work, without cmd=
How can I do the same in PHP?
I answered it myself after looking into the $_GET supervariable. It was easier than I thought.
function isCmd($cmd) {
if(isset($_GET["$cmd"]))
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the standard page</p>";
}
Also works with $_GET.
It would work with $_SERVER["QUERY_STRING"] too.
You can use $_SERVER["QUERY_STRING"] which will return the string after the "?" in the URL.
I have that code and it work fine:
if (isset($_POST['submit1']))
{
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
include_once('./token.php');
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php'); //**PAGE WITH SUBMIT1**
}
token.php
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
The form in my1page.php contains:
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>">
Now i need to nest a second if isset submit (token must be unset in the last submit).
WHAT I TRIED WITHOUT SUCCESS
if(isset($_POST['submit'])){
$_SESSION['submit']=true;
}
if (isset($_POST['submit']) || ( isset($_SESSION['submit']) && $_SESSION['submit'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit1'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit']=false;
include_once('./script/token.php');
include_once('./my3page.php');
} else {
header("location: ./3.php");
}
}
include_once('./my2page.php');
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
HTTP works stateless. That means that what is happening here is the following:
User calls this page for the first time. He sends a GET request so isset($_POST['submit1']) is false.
Now he clicks on submit and sends the first POST request. (I assume that you set a value for submit1 there.) isset($_POST['submit1']) is true and my2page.php gets returned.
He sends the third request. Again a POST request, but this time with a value for submit2. Your server template engine starts evaluating the php. isset($_POST['submit1']) is false, so it returns the old my1page.php
Basically, don't nest your checks, but use a it else instead. (Think of it as a switch/case
isset($_POST['submit1']) → ./my2page.php
isset($_POST['submit2']) → //end page
none → ./my1page.php
You can't have 2 submits in the same time so what happens here is
if(condition){
if(!condition){
//do somthing
}
}
this will never works try to use another page or i advice to save the first submit in the $_SESSION;
ADD this lign
$_SESSION['submit1'] = (isset($_POST['submit1']))? true: false;
than change the first condition
if (isset($_POST['submit1']) || $_SESSION['submit1']) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
//DO SOMETHINGS
} else {
header("location: ./index.php");
}
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
This is how it should be;
if(isset($_POST['submit1'])){
$_SESSION['submit1']=true;
}
if (isset($_POST['submit1']) || $_SESSION['submit1']) {
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
//DO SOMETHINGS
} else {
header("location: ./index.php");
}
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
Your close you could possible just change this
if (isset($_POST['submit1'],$_POST['submit2'])) { //check isset on both
if($_SESSION['user_token'] == $_POST['user_token']) {
if (isset($_POST['submit2'])) {
// if($_SESSION['user_token'] == $_POST['user_token']) { <--redundant check
unset($_SESSION['user_token']);
//DO SOMETHINGS
}
include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
} else {
header("location: ./index.php");
}
} else {
include_once('./token.php');
include_once('./my1page.php');
}
Depending on if you want an AND or an OR the above is equivalent to this
if (isset($_POST['submit1']) && isset($_POST['submit2'])) {
Obviously if you want an or then just put it here
if (isset($_POST['submit1']) || isset($_POST['submit2'])) {
It's not clear if you are talking about 2 POST's that are separate or concurrent
please check my script.
if($namachief != NULL)
{
echo $namachief;
}
else if ($namarm != NULL)
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}
My condition isn't working when in this condition => $namarm != NULL, I only get white page but its normal when in this condition => $namachief != NULL.
It's fine when i do this
echo $namachief; & echo $namarm;
if () {
}
else if (){
}
else if(){
------------------------ My Script Here -----------------------------
}
Both answer below are right. My problem lies here. In my form I did this
<option value="none">None</option> Then i change it to <option value="">None</option>
You try like this
if($namachief != NULL || $namachief != "")
{
echo $namachief;
}
else if ($namarm != NULL || $namarm != "")
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}
Write your condition as below:-
if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else{
echo "Something wrong. Please contact US";
}
If you want to print both variables if both are set and have valid values then,
if(empty($namarm) && empty($namachief)){
echo "Something wrong. Please contact US";
}
else if (!empty($namachief) && !empty($namarm)){
echo $namarm;
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else{
// else stuff
}
I'm working on a website and the index page checks if the user is logged in or not with this piece of code:
if (!$_SESSION['login'] && $_SESSION['login'] == "") {
include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");
} elseif ($_SESSION['login'] == 1) {
include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
}
But I want it to look cleaner, then I started wondering if was possible to achieve something like this with a function:
checklogin($_SESSION['login']);
I don't have much experience with functions, so i'm sorry if my question looks stupid, so thanks in advance.
Try this
if(check_login()) {
echo 'You are in!';
} else {
header('Location: login.php');
exit;
}
function check_login () {
if(isset($_SESSION['login'] && $_SESSION['login'] != '') {
return true;
} else {
false;
}
}
Just use empty:
if ( empty($_SESSION['login']) ) {
include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");
} else {
include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
}
Or condense it:
include_once $_SERVER['DOCUMENT_ROOT'].(empty($_SESSION['login']) ? "/login/" : "/main/");
There is what you need:
function userCheck()
{
return (isSet($_SESSION['login']) && $_SESSION['login']);
}
if(userCheck())
include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
else
include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");
Disregarding the fact of whether or not your approach makes sense, I think this would do what you expect:
function checklogin($login){
if (!$login && $login == "") {
include_once($_SERVER['DOCUMENT_ROOT'] . "/path/");
}
}
// **** call to the function
checklogin($_SESSION['login']);
// ****
You can use this function:
function checklogin() {
return (isset($_SESSION['login'])) ? true : false;
}
then on pages you want to check whether the user is logged in or not, you can:
if(checklogin() === true){
//here you would put what you want to do if the user is logged in
} else {
//this would be executed if user isn't logged in
header('Location: protected.php');
exit();
//the above would redirect the user
}
I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}