I have the following files
/includes
- file_inc1.php
- file_inc2.php
ajax.php
index.php
index.php calls an ajax request to ajax.php and ajax.php has something like below code
$nextStep = $_GET["nextStep"];
if($nextStep == 1) {
include "includes/file_inc1.php";
} else if($nextStep == 2) {
include "includes/file_inc2.php";
}
With this set-up, does anyone have a clue why the returned response from ajax is empty?
$nextStep = $_GET["nextStep"];
if($nextStep == '1') {
include "includes/file_inc1.php";
} else if($nextStep == '2') {
include "includes/file_inc2.php";
}
Thanks for all your responses. I was able to solve my own problem. I need to make sure that on my includes/file_inc1.php and includes/file_inc2.php files, I use ob_start() and ob_get_clean() like so:
<?php
ob_start();
// HTML + MYSQL + OTHERS
echo ob_get_clean();
?>
Related
I have following code in the start of my php file but somehow it doesnt redirect to login page, however if I separate if condition into sub if condition then redirection works
Below code doesnt work
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) && $_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit;
}
///This doesnt work
Below code work
if (!isset($_SESSION['emailid'], $_SESSION['roleid'])) {
header('location:login.php?lmsg=true');
exit();
}
if ($_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit();
}
///this works
can someone help?
This is easier to read and it's probably want you meant to do:
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) or (isset($_SESSION['roleid']) and $_SESSION['roleid'] != 1)) {
header('location:login.php?lmsg=true');
exit;
}
I am a beginner when it comes to php, and I have encountered a problem that I cannot find the solution for. I have tried searching for a relevant answer but I haven't found one.
I have the following code in my index.php:
<?php if ($set_status = 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
and this a bit further down:
<?php include 'scan/a.php'; ?>
And inside my a.php I have the following code:
<?php
$a = file_get_contents
('http://www.a-random-website/text.html', NULL, NULL, 2, 1);
if ($a == "0") {
include 'fail.php';
} elseif ($a == "1") {
include 'success.php';
} else {
echo 'Offline';
}
?>
And inside my fail.php I have the following code:
<?php
$set_status = 2;
echo 'Failed';
?>
So the idea here is that "a.php" will fetch a number from the website (The correct website has either ["1"] or ["0"] displayed that the code will fetch).
Depending on the result it returns, a.php will include either "fail.php" or "success.php", each containing either a success or a fail-message. If file_get_contents return a 0 I also want fail.php to $set_status = 2; which will cause "There is one or more errors" to be displayed on the front-page (index.php).
The reason that I am using include is that there's going to be a "b.php" and "c.php" and "d.php" and so on, all doing the same thing but fetching data from different pages. I want the success or fail-message to remain easy to edit, without having to edit each and every new x.php file.
So here's where it gets problematic. Everything works beautifully, except for the "There is one or more errors"-message that's supposed to trigger if ($set_status = 2).
I can get as far as the message showing, but when I switch the 1 and 0 in "a.php" (To simulate a specific result) the message will still show. I can't seem to figure it out.
So my question is: What have I done wrong, and what is the correct way to do it?
Thank you in advance!
Best regards,
Marc
use == operator in index.php to compare
<?php if ($set_status == 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
<?php if ($set_status ==2) {
echo 'There\'s one or more errors';
} else {
echo '';
}
?>
Just change this code ... see slash\ and == in the echo line .. else of your code id fine
I am using this if statement to redirect a user if the values in a .txt document is 0 but if it is 1 I want nothing to happen however I'm having some issues with my code.
This is my code currently:
$setup = require('setup.txt');
if ($setup === "0") {
echo '<script type="text/javascript"> window.location = "setup.php" </script>';
}
The setup.txt document currently contains the value 0.
I'd look here as to the proper usage of the require function.
if (file_get_contents('setup.txt') == "0") {
header('Location: /setup.php');
}
Use the header function if you have not already sent output to the browser.
$setup = file_get_contents('setup.txt');
if ($setup == "0") {
header('Location: /setup.php');
}
Since all PHP is executed before the output the site. Use this option first.
You can not use include() / require() to transfer as a variable, like you have. Use file_get_contents() to achieve the results.
try this:
<?php
$setup = file_get_contents('setup.txt');
if (trim($setup) == "0") {
echo '<script type="text/javascript"> window.location = "setup.php" </script>';
}
?>
I need some help with this I have for example index.php and and i need to make it something like.
someone access:
index.php?search=blbla
include search.php
else
include home.php
I need an advice with this thanks
Try this
if (isset($_GET['search'])) include('search.php');
else include('home.php');
Well, you could use isset() to see if the variable is set. e.g.
if(isset($_GET['search'])){
include "search.php";
}
else {
include "home.php";
}
$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}
I personally prefer to check if a $_GET is set and if it actually equals something like so:
if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php';
else: include 'home.php';
This will avoid the problem of putting in the $_GET variable but not actually setting it.
use it like this
if (isset($_GET['search']))
include 'search.php';
else
include 'home.php';
<?php
//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
include ('search.php');
}else{
include ('home.php');
}
?>
When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.
So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...
if (strlen(trim($_GET['search'])) > 0) {
include('search.php');
} else {
include('home.php');
}
Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".
Here is an example of what I am trying to do:
index.php
<ul><?php include("list.php") ?></ul>
list.php
<?php
if (PAGE_NAME is index.php) {
//Do something
}
else {
//Do something
}
?>
How can I get the name of the file that is including the list.php script (PAGE_NAME)? I have tried basename(__FILE__), but that gives me list.php.
$_SERVER["PHP_SELF"]; returns what you want
If you really need to know what file the current one has been included from - this is the solution:
$trace = debug_backtrace();
$from_index = false;
if (isset($trace[0])) {
$file = basename($trace[0]['file']);
if ($file == 'index.php') {
$from_index = true;
}
}
if ($from_index) {
// Do something
} else {
// Do something else
}
In case someone got here from search engine, the accepted answer will work only if the script is in server root directory, as PHP_SELF is filename with path relative to the server root. So the universal solution is
basename($_SERVER['PHP_SELF'])
Also keep in mind, that this returns the top script, for example if you have a script and include a file, and then in included file include another file and try this, you will get the name of the first script, not the second.
In the code including list.php, before you include, you can set a variable called $this_page and then list.php can see the test for the value of $this_page and act accordingly.
Perhaps you can do something like the following:
<ul>
<?php
$page_name = 'index';
include("list.php")
?>
</ul>
list.php
<?php
if ($pagename == 'index') {
//Do something
}
else {
//Do something
}
?>
The solution basename($_SERVER['PHP_SELF']) works but I recommend to put a strtolower(basename($_SERVER['PHP_SELF'])) to check 'Index.php' or 'index.php' mistakes.
But if you want an alternative you can do:
<?php if (strtolower(basename($_SERVER['SCRIPT_FILENAME'], '.php')) === 'index'): ?>.