How to remove the included file in PHP - php

I"m loading a file in start of PHP. I have declared some activity and assign them to buttons which are aligned left and right side of HTML document. On click of each button will included some Other PHP file but I want to remove the previous loaded on startup file before including any other file.
Here is my code
<?php
// php file on startup of file
include("dashboard.php");
$activity = $_REQUEST['activity'];
if($activity) {
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
if($activity == 'dashboard'){
}
if($activity == 'issueBooks'){
include("issueBooks.php");
}
if($activity == 'returnBooks'){
include("returnBooks.php");
}
<?php
I tried with
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
But since I"m very new to PHP, it didn't work as expected.
Any Help anyone.

That isn't how include works. When you include a file, the contents of that file are executed at that point in the code. There's no straightforward way to undo that.
It looks like you want the dashboard activity to be the default view if no activity has been selected yet. To do that, you can just include it only when there is no activity value in request, or when the dashboard activity has been specifically requested.
if (empty($_REQUEST['activity']) || $_REQUEST['activity'] == 'dashboard') {
include 'dashboard.php';
}

Related

PHP site problems

My code of file.php:
<?php
if(!isset($_GET['filename']) OR $_GET['filename'] == NULL) {
print("Error!");
exit();
}
$_GET['filename'] = htmlentities($_GET['filename'], ENT_QUOTES, "utf-8");
session_start();
include_once("/var/www/html/get.php");
for($i = 0; $i < $GLOBALS['files']['ile']; $i++) {
if($GLOBALS['files'][$i]['name'] == $_GET['filename']) {
if($GLOBALS['files'][$i]['priv'] == NULL OR $GLOBALS['files'][$i]['owner'] == $_SESSION['id'] OR (isset($_SESSION['privs']) AND in_array($GLOBALS['files'][$i]['priv'], $_SESSION['privs']))) {
if(file_exists($GLOBALS['files'][$i]['loc'])) {
header("Content-length: ".filesize($GLOBALS['files'][$i]['loc']));
header("Content-type: ".mime_content_type($GLOBALS['files'][$i]['loc']));
readfile($GLOBALS['files'][$i]['loc']);
} else {
print("Can't find that file!");
}
}
}
}
?>
In get.php file, I loads (from database) information about files I wanted to have access using that file above in site.
$_SESSION['privs'] //it's an array that holds privileges, i.e: site.priv.mess
$GLOBALS['files'] //holds info about all files that user can load, i.e: $GLOBALS['files'][0]['name'] is a name of first file in array
$GLOBALS['files'][0]['loc'] //holds info about first file localisation
$GLOBALS['files']['ile'] // holds sizeof($GLOBALS['files'])
With pictures that works well, but if I try to load larger file, i.e. video that weights 300MB, then file loads, all looks good, but if I reloads site, it won't work anymore...
I tried to delete my cookies in browser (to change my session ID) and it works... But what can I do to make it works better?
EDIT: On Firefox all looks good, only freezes on Chrome :(
EDIT2: Closing session with: session_write_close(); before reading file fixed my curse :P Thanks y'all
Check your php.ini or use phpinfo() to check your values for upload_max_filesize, post_max_size and memory_limit. Maybe also check the max execution time of the php script.

Combine php cookie check with request uri

I'm looking for a way to combine this working script with another check: request uri ?=en or ?=nl. I need this because a php include according to the active language cookie (nl or en) doesn't change immediately after you press the language button. The buttons bring you to [].php?=en - which in fact just refreshes the page. To see the php include in the correct language, you need to refresh the page one more time. Thats a problem. Can anybody help me?
<?php
if( $_COOKIE['lang'] == "nl"){
include ('statement_nl.php');
}
else{
include ('statement_en.php');
}
?>
EDIT:
After a few hours of puzzling I found a way to make it work:
<?php
if( $_COOKIE['lang'] == "nl"){
include ('statement_nl.php'); }
else if (isset($_GET["lang"]) AND ($_GET["lang"]=="nl")) {
include ('statement_nl.php'); }
else if (isset($_GET["lang"]) AND ($_GET["lang"]=="en")) {
include ('statement_en.php'); }
else{
include ('statement_en.php'); }
?>

Update_Comment_meta is not defined

I am working with WordPress plugin, and I am doing following.
Create a new button in comments.
Click here
Now on magic.php I am calling update_comment_meta.
$decode_str=base64_decode($_GET['en']);
if(substr($decode_str, 0,4) != "http"){
$decode_str = "http://".$decode_str;
}
if(is_numeric($_GET['cid']) && is_numeric($_GET['pid']) ){
update_comment_meta( $_GET['pid'] , 'pinned', $_GET['cid'] );
}
header("Location: ".$decode_str);
?>
It givens me error, update_comment_meta is not defined.
What I think is I might need to load wp-load.php, because magic.php all alone doesn't know it is a part of wordpress.
I don't want to do AJAX. I have also read loading wp-load.php within your file is not a good idea.
Thanks.

passing variables between functions and files in php

I have a file called admin.php in which I have a button with the name send. What I want to do is when I click it, to make visible a link on the user's page, user.php. How can I do this?
I have a file with all my functions called functions.php in which I have a function called onSubmit($var); I initialize the variable $var is admin.php with the value $_POST['send'] but when I call the function in the file user.php I have no way of telling him who the variable $var is so I get 'undefined index'.
Is there another way to do this?
EDIT Added code
This is admin.php
<input type="button" name="send" value="Submit" /><br/>
require 'functions.php';
$posted = $_POST['send'];
onSubmit($posted);
This is user.php
require 'functions.php';
onSubmit($var); //here it says undefined index var because it doesn't know who the variable is
if($isSent == 1) {
<a style="visibility:visible;" href="test3.html" id="test3">Test3</a> <br/>
}
And this is functions.php
global $isSent;
function onSubmit($var) {
if(isset($var)) {
$isSent = 1;
}
}
Basically you need to use sessions like below:
if(isset($_SESSION['makeVisible']) && $_SESSION['makeVisible'] == true){
echo '<button>Button init</button>'; //you could also use html like the comment below.
}
/*
if(condition){?> <!-- this is now html --> <button>Button init</button><?}
*/
Then to set this variable on your admin page use:
if(isset($_POST['submitButton'])){
$_SESSION['makeVisible'] == true;
}
You'll also need a form for this method to work but there are other methods but I prefer this one.
<form name="buttonMakerThing" method="POST">
<input name="submitButton" value="Make button init Visible" type="submit"/>
</form>
Without an action the form defaults to 'POSTING' the form information to the current page. Making the condition if(isset($_POST)) return true.
You will need to add a $_SESSION declaration at the top of every php page you have on your site for this to work. It MUST go on the very first line of every page! for example:
01: | <?php session_start();
02: |//rest of script;
Please look more into $_SESSIONS for unnsetting/destroying your sessions and more uses for them :) http://php.net/manual/en/reserved.variables.session.php
Right I've done a bit of research on Caching and this is what I've come up with. It might not be 100% correct but it's a start as like I've said I've never tried it myself lol
In your admin.php I'd put this function in:
if(isset($_POST['send'])){
if($enabled == true){
$enabled == false;
}
else{
$enabled == true;
}
apc_add('enabled',$enabled);
}
Now to 'get' our $enabled var:
$enabled = apc_fetch('enabled');
Then to check the the var within your client page:
if($enabled == true){
echo ' button';
}
Now the only things I haven't fully looked at is the security of the apc_ function and the client usage. I believe it works for all clients of the server but I'm not 100% certain. Here the php manual to give better examples.
This is the method I was thinking of. But again I'm not sure on the security of it but I'm sure you can find something to keep it secure. The video is actually is tutorial for a Youtube API. But he does cover saving a variable to a cache text file which should be of use to you :)
If you have functions.php which defines functions, simply include it in admin.php file and then you can call the function from there and also pass value.

Browser displays some code instead of page - smarty template

The problem is that I downloaded a web page to my local server (via ftp).
Now the browser displays some code instead of a page.
The website is running on smarty templates, but that may or may not be the issue.
I tried to track the issue and what I know already is that the displayed code is a part from main index.php file and looks like this:
=$_var['time'] && $output[$x]['tags']['state'][0][0]==1){ $_out['box']['promotions'][]=$output[$x]; } } else if ($_GET['state'] == 1) { if($output[$x]['tags']['state'][1][1]<=$_var['time'] && $output[$x]['tags']['state'][1][2]>=$_var['time'] && $output[$x]['tags']['state'][1][0]==1){ $_out['box']['news'][]=$output[$x]; } } } $_out['prodlimit']['news'] = 24; $_out['prodlimit']['promotions'] = 24; } else { for($x=0;$x=$_var['time'] && $output[$x]['tags']['state'][0][0]==1){ $_out['box']['promotions'][]=$output[$x]; } if($output[$x]['tags']['state'][1][1]<=$_var['time'] && $output[$x]['tags']['state'][1][2]>=$_var['time'] && $output[$x]['tags']['state'][1][0]==1){ $_out['box']['news'][]=$output[$x]; } } $_out['prodlimit']['news'] = 12; $_out['prodlimit']['promotions'] = 4; } // filtry $query1="select * from ".$_base[prefix]."mod_assortment_filters order by category,position,id"; $result1=mysql_query($query1); while($dane1=mysql_fetch_assoc($result1)){ $query2="select * from ".$_base[prefix]."images where module='assortment_filters' && parent='$dane1[id]' order by id limit 0,1"; $result2=mysql_query($query2); if($dane2=mysql_fetch_assoc($result2)){ $dane1[logo]=$dane2; } $_out[filters][strtolower($dane1[category])][]=$dane1; } //print_r($_out[filters]); // slideshow $query="select * from ".$_base[prefix]."mod_component_files where name like 'Slideshow:%' && visible='1'"; $result=mysql_query($query); while($dane=mysql_fetch_assoc($result)){ $_out[slideshow][]=$dane; } // smarty $smarty->assign("_out",$_out); $smarty->assign("informations",$informations); $smarty->assign('content_cell', $content_cell); $smarty->display("index.tpl"); ?>
The part before it in the index.php file is:
if(isset($_GET['state'])) {
for($x=0;$x<count($output);$x++){
if ($_GET['state'] == 0) {
if($output[$x]['tags']['state'][0][1]<
There are also some variable declarations earlier.
Actually any changes have no effect until I change something in the index.php - which generally results in the less code displayed by the browser.
Could this be some php version differences between my local server and the web server (web-server has older php version I guess)?
I am clueless - I need this working so I could restyle it - it is a e-commerce web page with database etc..
Thanks in advance.
Try to check the links and paths to all files. This is something that normally IDE's don't highlight as errors and may cause some problems.
I found the solution. My php.ini was set to not allow short tag forms,

Categories