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".
Related
I have a URL e.g. http://foo.com?stone=yes
The page pointing to that URL is coded in PHP.
I can use the following to read stone.
I want to be able to redirect the page if:
stone's value is null
stone isn't present in the URL.
I've tried the following but it's not working.
$stonevar = $_GET['stone'];
if ($stonevar = "") {
header("Location: http://google.com");
}
You can do that with empty()
<?php
if(empty($_GET['stone'])) {
header("Location: http://google.com");
exit;
}
?>
You have some erros in your code.
1- If you don't check if the $_GET['stone'] exist you will get a warning like: Undefine variable...
2- In your if stament you're assigning a value, to compare values use == or === instead.
Change your code to:
$stonevar = isset($_GET['stone']) ? $_GET['stone'] : NULL;
if (empty($stonevar)) {
header("Location: http://google.com");
exit();
}
Useful links:
isset
empty
Comparison Operators
Ternary Operator
The code $stonevar = "" in your if condition is assigning the value "" to the variable $stonevar.
For comparison, you will want to use the equality operator ==, e.g.:
if ($stonevar == "") {
header("Location: http://google.com");
}
<?php
if(empty($_GET['stone'])) {
header("Location: http://google.com");
}
?>
I have a main page that users go to that shows output from a MySQL query depending on the variable passed to it. So;
http://website/mypage.php?page=0
However, I would like to set up redirection so that if someone just goes to
http://website/mypage.php
that it will go to http://website/mypage.php?page=0. I thought of using the following code, which verifies the current page as well as verifies that a user's session is established;
elseif ($_SERVER['PHP_SELF'] == '/mypage.php' && isset($_SESSION['valid_user']))
{
header('Refresh: 0; URL=/mypage.php?page=0');
}
But, this looks to be too general. Is there a way to check for exactly '/mypage.php' or maybe '/mypage.php?page=' ? I thought of using strlen to check for only the 11 characters in /mypage.php, but I'm not sure that this is the most efficient way of doing it.
You can check to see if the variable page has a value and if its empty you can do the redirect
if($_GET['page'] == ''){
header('Location: /mypage.php?page=0');
exit;
}
or
if(!isset($_GET['page'])){
header('Location: /mypage.php?page=0');
exit;
}
In your mypage.php you can check something like this
if(!isset($_GET['page'])) {
header('location: /mypage.php?page=0');
exit;
}
But, I think, instead of redirecting to same page with a get variable, why don't just show the page you want to show by default, when there is no page variable is set.
You should use this way:
header('Location: /mypage.php?page=0');
exit;
Otherwise check the $_SERVER variables for more strict match. http://php.net/manual/en/reserved.variables.server.php
I think probably you need $_SERVER['REQUEST_URI']
Is this what you're looking for?
if(!isset($_GET['page']) || empty($_GET['page'])) {
Header('Location: http://website/mypage.php?page=0')
exit();
}
or
if(!isset($_GET['page']))
$_GET['page'] = 0;
I'm not sure about the second solution, you shouldn't attribute value to $_GET[] variables.
if( !isset($_REQUEST['page']) ) {
header('Location: /mypage.php?page=0');
exit(0);
} else {
if( $_REQUEST['page']=="" ) {
header('Location: /mypage.php?page=0');
exit(0);
}
}
On running the following code I'm getting the following error:
Notice: Undefined index: hid in E:\Program Files\xampp\htdocs\cont.php
echo "<table align='right'><tr><td><a href='cont.php?hid=101'><input type='button' value='Account Settings'></a></td></tr></table>";
cont.php code:
$con = mysql_connect("localhost","Rahul","");
mysql_select_db("ebusiness", $con);
if($_SESSION['id']==1)
{
include 'business.php';
}
else if($_GET['hid']==101)
{
session_start();
include 'edprofile.php';
}
You are directly checking on $_GET['hid'] without checking if it is set or not.
else if(isset($_GET['hid']) && $_GET['hid'] == 101)
Try this code :
added : $hid = isset($_GET['hid'])?$_GET['hid']:"";
edited : $_GET['hid'] to else if($hid==101)
$con = mysql_connect("localhost","Rahul","");
mysql_select_db("ebusiness", $con);
$hid = isset($_GET['hid'])?$_GET['hid']:"";
if($_SESSION['id']==1)
{
include 'business.php';
}
else if($hid==101)
{
session_start();
include 'edprofile.php';
}
Change this line:
else if($_GET['hid']==101)
to:
else if(isset($_GET['hid']) && $_GET['hid']==101)
Remember that when you are using GET or POST on your page, there is no guarantee that a certain variable has been sent, as the HTTP protocol is stateless by default.
It is there for important to checn that a variable exists, and also that the variable is validated. To this end the isset() method is quite useful. I would recommend you do a
isset($_GET['hid'])
in your code in an if statement.
you are also missing to pass hid variable in you get request ( in URL). check it out. It shuld content ?hid=101 or &hid=101 some where.
and replace your else if condition with
else if(isset($_GET['hid']) && $_GET['hid']==101)
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'): ?>.
Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}