if user is blocked php include page, else include other? - php

Can someone please help me, i am trying to incorporate this piece of code:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
to fit this piece of code as an else statement:
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
I have a table in my database that puts a users block status from 0 to 1. and if a user blocks someone and that user tries to access their profile then i am trying to make it so that the user goes to another page that says blocked. i am doing this through <?php include(.. ?>
At the moment i just tried putting this at the top of the page:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
and whilst it is working and including the page mod_blocked.php its also bringing up mod_profile.php which is the default profile page and overlapping. So basically if a users not blocked they should go to mod_profile.php and if a users blocked they go to mod_blocked.php.
can someone please show me where im going wrong and how to achieve this?
Here's the entire page of code:
<?php
$page_title = "Profile";
include('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
?>
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
<?php
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
}
else if (!isset($profile_id)) {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
else if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include("includes/mod_profile/mod_account.php");
}
?>

I'm not sure if I fully understand what you are up to, but when I got your question correct, you could do it the following way:
$page_title = "Profile";
include('includes/headerframe.php');
// stores the correct include file...
$toInclude = false;
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
// look up for blocked user..
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
// don't do the include inside the loop
// we simply set the $toInclude
$toInclude = "includes/mod_profile/mod_blocked.php";
break;
}
}
$user_info_set = get_user_info();
// in case no include is set, and there is no profile...
if ( !$toInclude && (!$user = mysql_fetch_array($user_info_set) ||
!isset($profile_id)) ) {
// we set the $toInclude now
$toInclude = 'includes/mod_profile/mod_noprofile.php';
}
if($toInclude) {
// either blocked or no-profile
include($toInclude);
} else {
// user is not blocked, and you have a profile
// proceed with your code for normal user handling here
}

Couple options here.
Add break or die() like such
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
break;
}
}
?>
Or, surround it in the else
<?php
$page_title = "Profile";
include ('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset($_GET['id'])) {
$profile_id = $_GET['id'];
die();
}
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_blocked.php");
} else {
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
} else if (!isset($profile_id)) {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include ("includes/mod_profile/mod_profile.php");
} else if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include ("includes/mod_profile/mod_account.php");
}
}
}
?>

Related

How to set OR in isset_POST

I want to set OR in isset($_POST), so that there will be only one button to save in database.
btnusersave is the one that will save the whole data in dialog,
while saveuseraccess button is the one that saves the check boxes in database.
This is my code for btnusersave
<?php
$eventrequirements = 'unchecked';
if (isset($_POST['btnusersave']))
{
if (isset($_POST['eventrequirements']))
{
$eventrequirements = $_POST['eventrequirements'];
if ($eventrequirements == 'net') {
$eventrequirements = 'checked';
}
}
}
?>
Perhaps something like this?
<?php
$eventrequirements = 'unchecked';
if (isset($_POST['btnusersave'])) {
if (isset($_POST['eventrequirements'])) {
$eventrequirements = $_POST['eventrequirements'];
if ($eventrequirements == 'net') {
$eventrequirements = 'checked';
}
}
}else if ( isset($_POST['saveuseraccess']) ) {
//do alternate code (to save checkboxes) here
}
?>

Putting a session in a if and else statement

I've got a problem with putting a session to store some page info into a variable
heres the code:
<?php
$t = $_GET['nm'];
if ($t=="1")
{
session_start();
// store session data
$_SESSION['nm']=1;
}
else
{
?>
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "http://www.gouda-info.nl/mobile";
}
</script>
<?php
}
$session = $_SESSION['nm'];
if ($session=="1")
{
When i try to use the script it just doesn't work. I use this script to redirect mobile users, but if they choose to use the Desktop version they'll be allowed by activating the session that stores if the user has activated the desktop version by putting nothing or a 1 in the link like so:
http://www.example.com/index.php?nm=1
hope anyone comes up with a bright solution. :)
EDIT:
it just fails if i try to run this code, it gives me a blank page.
session must be started on the top, and sometimes you deal with == 1 and other with $t == "1"
try this code:
// first line
session_start();
$t = $_GET['nm'];
if ($t == 1) { // use 1 instead of "1"
// store session data
$_SESSION['nm'] = 1;
} else {
?>
<script>
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
window.location = "http://www.gouda-info.nl/mobile";
}
</script>
<?php
}
$session = $_SESSION['nm'];
if ($session == 1) { // use 1 instead of "1"
}
You are using js code in php, but your js will be run after entire php file executed. So use php instead;
<?php
session_start();
$t = $_GET['nm'];
if ($t == "1") {
// store session data
$_SESSION['nm'] = "1";
} else {
if(isMobile()) {
header('Location: http://www.gouda-info.nl/mobile');
exit();
}
}
$session = $_SESSION['nm'];
if ($session == "1") {
......
}
function isMobile($user_agent=NULL) {
if(!isset($user_agent)) {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}
return (strpos($user_agent, 'Android') !== FALSE
|| strpos($user_agent, 'webOS') !== FALSE
|| strpos($user_agent, 'iPhone') !== FALSE
|| strpos($user_agent, 'iPad') !== FALSE
|| strpos($user_agent, 'iPod') !== FALSE
|| strpos($user_agent, 'BlackBerry') !== FALSE);
}

PHP Issue - function output is 000

I've been trying to make a CLI application that logs Yahoo! messenger login dates/times for certain users using a third party, but this isn't really getting anywhere. Even though checking iself works when used individually, it does not seem to when using the while & foreach too. checkAvailability outputs "000". Could anyone please fix this and perhaps optimize it?
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
echo $status_coded;
return $status_coded;
}
while(true) {
foreach($users as $user) {
$user['oldstatus'] = $user['status'];
if (checkAvailability($user) == "1" and $user['oldstatus'] != "online") {
$user['status'] = "online";
echo "online";
} elseif (checkAvailability($user) == "3" and $user['oldstatus'] != "invisible") {
$user['status'] = "invisible";
echo "invisible";
} elseif (checkAvailability($user) == "2" and $user['oldstatus'] != "offline") {
$user['status'] = "offline";
echo "offline";
} else {
$user['status'] = "error";
echo "error";
}
if ($user['status'] != $user['oldstatus']) {
echo $user." a fost detectat ca ".$user['status']." la ".date(DATE_RFC822).".\n";
}
}
sleep(60);
sleep(60);
}
An endless runing CLI application in PHP in not the best solution. Its better when you make a cronjob and run the script every one or every to minutes. Then you can store the status or what you need in a database or a file.
I have looked at your scripted and test the script:
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
return $status_coded;
}
while(true) {
foreach($users as $key => $user) {
$userStatus[$key] = checkAvailability($user);
if(!isset($userStatusRet[$key]['oldstatus'])) {
$userStatusRet[$key]['oldstatus'] = '';
}
if(!isset($userStatusRet[$key]['status'])) {
$userStatusRet[$key]['status'] = '';
}
$userStatusRet[$key]['oldstatus'] = $userStatusRet[$key]['status'];
if ($userStatus[$key] == "1" and $userStatusRet[$key]['oldstatus'] != "online") {
$userStatusRet['status'] = "online";
echo "User ".$key.": online\n";
} elseif ($userStatus[$key] == "3" and $userStatusRet[$key]['oldstatus'] != "invisible") {
$userStats['status'] = "invisible";
echo "User ".$key.": invisible\n";
} elseif ($userStatus[$key] == "2" and $userStatusRet[$key]['oldstatus'] != "offline") {
$userStatusRet[$key]['status'] = "offline";
echo "User ".$key.": offline\n";
} else {
$userStatusRet[$key]['status'] = "error";
echo "User ".$key.": error\n";
}
}
sleep(5);
}
Its not the best solution but the problem in your case is, that you can't write in the $user output. I have made here a new variable with the user id as iterator. When you run the script you can see the output:
User 0: online
User 1: offline
User 0: online
User 1: error
User 0: online
User 1: offline

PHP IF Else Function Not Working

I have a problem, I can't get the result of my code. When I try to debug the code. I using IF statement in my code.
It's just simple, example : if kelas(based on user_id login) = 1, then redirect it to kelas1.php, if kelas = 2, the redirect to kelas 2, else no have a kelas.
Here it's my code :
<?php
session_start();
if(!isset($_SESSION['user_id'])) {
header('Location: form_login_siswa.html');
}
$user_id = $_SESSION['user_id'];
include ("config.php");
$query = "SELECT kelas FROM t_siswa WHERE user_id = '$user_id'";
$hasil = mysql_query($query);
while ($data = mysql_fetch_array($hasil)) {
$kelas = $data['kelas'];
if($kelas = 1) {
include ("kelas1.php");
}
if($kelas = 2) {
include ("kelas2.php");
} else {
echo "Tidak ada kelas";
}
}
?>
Anyone, please help to solve the problem.
Appreciated with your helps.
Thank you.
You're missing an else and also not using the correct operator for comparison.
Also, switch around the comparison so that the first value is always a valid value.
if(1 == $kelas)
{
include ("kelas1.php");
}
else if(2 == $kelas)
{
include ("kelas2.php");
}
else
{
echo "Tidak ada kelas";
}
$kelas = 1
is assignment
$kelas == 1
is comparison
if($kelas = 1)
should be
if($kelas == 1)
Try using == instead of = . The "=" is an assignment operator and the expression gets the value of the right operand.
if($kelas == 1)
{
include ("kelas1.php");
}
if($kelas == 2)
{
include ("kelas2.php");
}
else
{
echo "Tidak ada kelas";
}
Also, the statement will always echo "Tidak ada kelas"; if ($kelas == 1). Was this intentional?
In your if statements using == for testing comparison rather than single = which is for assignment
Take a look at the elsif structure.
while ($data = mysql_fetch_array($hasil)) {
$kelas = $data['kelas'];
if($kelas == 1) {
include ("kelas1.php");
} elseif($kelas == 2) {
include ("kelas2.php");
} else {
echo "Tidak ada kelas";
}
}
Or the switch structure:
while ($data = mysql_fetch_array($hasil)) {
switch($data['kelas']){
case 1:
include ("kelas1.php");
break;
case 2:
include ("kelas2.php");
break;
default:
echo "Tidak ada kelas";
}
}

Making code more efficient and less a copy and paste

I've been working on a piece of code that that pulls the name of a guild and with it the information of boss/monsters said guild has killed in an online game. There are many many monsters in this game and every one has three difficulty settings. I have managed to get the code to do what i want however it has an enormous amount of copy and paste and ive only done about 1/5 of the total amount of enteries. I really cant think how to make this code less of a giant bloat. This is the code for just one monster for the 3 difficulty settings as you can see it's alot just for one. there are probably another 60 of these!. Can anybody help me understand better ways to do this. Thanks!
$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
/////////////////////////////////// START - 8 MAN BONETHRASHER
$normal = '';
$hard = '';
$nightmare = '';
/////// START - CHECK NORMAL
if ($row['pf_kp_em_no_bonethr'] == '1')
{
$normal = ' <img src="/styles/subsilver2/theme/images/soap/no.png" />';
}
else if ($row['pf_kp_em_no_bonethr'] == '2')
{
$normal = '';
}
else if (is_null($row['pf_kp_em_no_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NORMAL
/////// START - CHECK HARD
if ($row['pf_kp_em_ha_bonethr'] == '1')
{
$hard = ' <img src="/styles/subsilver2/theme/images/soap/ha.png" />';
}
else if ($row['pf_kp_em_ha_bonethr'] == '2')
{
$hard = '';
}
else if (is_null($row['pf_kp_em_ha_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK HARD
/////// START - CHECK NIGHTMARE
if ($row['pf_kp_em_kn_bonethr'] == '1')
{
$nightmare =' <img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}
else if ($row['pf_kp_em_kn_bonethr'] == '2')
{
$nightmare = '';
}
else if (is_null($row['pf_kp_em_kn_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NIGHTMARE
if ($normal == '' && $hard == '' && $nightmare == '')
{
}
else
{
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}
}
$db->sql_freeresult($result);
I'm still slightly fuzzy at what you are trying to do, but I'll give helping you out a shot.
You could probably get away will creating a class that does all of this.
For example:
class checks {
public function checkBosses($normalBoss, $hardBoss, $nightmareBoss) {
$difficulties = array();
$difficulties['normal'] = array('boss' => $normalBoss);
$difficulties['hard'] = array('boss' => $hardBoss);
$difficulties['nightmare'] = array('boss' => $nightmareBoss);
foreach ($this->difficulties as $difficulty -> $boss) {
$this->difficulties[$difficulty]['result'] = checkDifficulty($boss['boss'], $difficulty);
}
$normal = $this->difficulties['normal']['result'];
$hard = $this->difficulties['hard']['result'];
$nightmare = $this->difficulties['nightmare']['result'];
if ($normal == '' && $hard == '' && $nightmare == '') {
return null;
} else {
return array(
'normal' => $normal,
'hard' => $hard,
'nightmare' => $nightmare,
);
}
}
protected function checkDifficulty($boss, $difficulty) {
if ($difficulty == 'normal') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/no.png" />';
} else if ($difficulty == 'hard') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/ha.png" />';
} else if ($difficulty == 'nightmare') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}
if ($boss == '1') {
return $image;
} else if ($boss == '2') {
return '';
} else if (is_null($boss)) {
echo "Boss was set as NULL This should not happen!";
} else {
echo "Sosia messed up go hit him in the face.";
}
}
}
Then all you would need to do is call:
$checkResult = checks::checkBosses($row['pf_kp_em_no_bonethr'], $row['pf_kp_em_ha_bonethr'], $row['pf_kp_em_kn_bonethr']);
if ($checkResult != null) {
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}
If you can retrieve an array of bosses, you can do a foreach loop on them to run that same bit of code for each boss like this:
foreach ($bosses as $boss) {
//Full code to be repeated for each boss here
}

Categories