Including files from different folders include ("./".$_GET['id'].".php"); - php

hi everybody just found this code
<?PHP
if (isset($_GET['id'])) {
if (!empty($_GET['id']) && $_GET['id'] != "index") {
if (file_exists($_GET['id'].".php")) {
include ("./".$_GET['id'].".php");
} else {
echo "Not Found section";
}
} else {
include ("start.php");
}
} else {
include ("start.php");
} ?>
Gallery
i want use it , but i have my files in another folder: "FILESPHP", how i can make link to FILESPHP folder? .. thankyou.

Consider using google before.
with getcwd() you get your current directory
you can navigate from there as in every linux
Kind of:
<?PHP
if (isset($_GET['id'])) {
if (!empty($_GET['id']) && $_GET['id'] != "index") {
if (file_exists($_GET['id'].".php")) {
include ("./FILESPHP/".$_GET['id'].".php");
} else {
echo "Not Found section";
}
} else {
include ("FILESPHP/start.php");
}
} else {
include ("FILESPHP/start.php");
} ?>

thank you inetphantom,
when refresh page.. FILESPHP/start.php load very GOOD..
but.. when i call files in FILESPHP.. not works..
the example is: Gallery show "Not Found section"

Related

Changing links by priority - PHP / HTML

I am currently working on a school project and appreciate your help.
HTML Code:
LINK 1
LINK 2
LINK 3
Now as I am relatively new to PHP, I need clue wehere to start or which topics are relevant for my solution. I have already searched the Forum here and with google, but unforutenly, didn't find anything, that fits my needs.
What I need, is a PHP-Code, that contains 6 href Links and every Link has a priority. So every time somebody clicks on "LINK 1" on the html file, he gets directed to the PHP-File "change-1.php", where one of the six href Links get activated.
PHP CODE "change-1.php":
<?php
if (condition) {
echo "http://www.someotherwebsite-1.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-2.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-3.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-4.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-5.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-6.com";
}
?>
Perhaps this should help
LINK 1
LINK 2
LINK 3
Just create one change.php file
//to get change
<?php
if ($_GET['change'] == 1) {
echo "http://www.someotherwebsite-1.com";
}
elseif ($_GET['change'] == 2) {
echo "http://www.someotherwebsite-2.com";
}
elseif ($_GET['change'] == 3) {
echo "http://www.someotherwebsite-3.com";
}
elseif ($_GET['change'] == 4) {
echo "http://www.someotherwebsite-4.com";
}
elseif ($_GET['change'] == 5) {
echo "http://www.someotherwebsite-5.com";
}
elseif ($_GET['change'] == 6) {
echo "http://www.someotherwebsite-6.com";
}
?>
Now this is the basic idea. You can manipulate the data however you want.
I hope you got the concept.

How to properly redirect to the same page in php

I am working on something that i want to use in a project.
i am to redirect the current page to itself using php and session. The idea is that:
if my `$_session['insert']= true; then refresh the page to itself;`
Here is the code:
sess_page1.php
session_start();
$_SESSION["insert"] = 1;
$_SESSION["test"] = "Page refreshed";
?>
Click here to go to the next page
sess_page2.php
<?php
session_start();
if (isset($_SESSION["insert"]) && ($_SESSION["insert"]==1)) {
header('location:'.$_SERVER['PHP_SELF']);
print $_SESSION["test"];
}
else
{
echo "Impossible to execute.";
exit();
}
?>
But the page does not redirect to itself...The whole idea is the page should refresh one time.
What i did wrong here? Can somebody help?
Try this:
sess_page1.php
session_start();
$_SESSION["stopper"] = 0;
$_SESSION["insert"] = 1;
$_SESSION["test"] = "Page refreshed";
?>
Click here to go to the next page
sess_page2.php
<?php
session_start();
if (isset($_SESSION["insert"]) && ($_SESSION["insert"]==1) && ($_SESSION["stopper"]==0)) {
$_SESSION['stopper']=1;
header('location:'.$_SERVER['PHP_SELF']);
print $_SESSION["test"];
}
else
{
echo "Impossible to execute.";
exit();
}
?>

GET request variable error in PHP

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

How to exec the shell script in PHP

I'm making PHP script to move shell script. when hoge < 50.
Code is this. Does it OK?
When I use that script , I click this link.
http://localhost/index.php?hoge=30
<?php
if(isset($_GET['hoge'])) {
$hoge = $_GET['hoge'];
print("$hoge<br>\n");
}
if ($hoge > 50 ){
echo 'F1';
}
elseif ($hoge == 50) {
echo 'F2';
} else {
echo 'F3';
exec(' /Users/hoge/Desktop/test.sh');
}
?>
You can add a test like
if (file_exists('/Users/hoge/Desktop/test.sh')) {
exec('/Users/hoge/Desktop/test.sh');
} else {
echo 'NO FILE'
}
Check also that the file /Users/hoge/Desktop/test.sh is executable

Wordpress if 404 title

I have a wordpress plugin that has the following code inside header.php:
<?php
if (the_subtitle("","", false) == "") {
the_title();
} elseif(is_404()) {
echo "404";
} else {
the_subtitle();
}
?>
Basically what should happen is:
If subtitle present, echo subtitle.
If no subtitle present, echo title.
If 404, echo "404".
But for some reason, when I locate my 404.php page, their is nothing displayed?
If your first check is for subtitle and your 404-page has no subtitle then that part of the if-statement is triggered and all other checks are skipped. By performing the 404 check first things should work as expected.
<?php
if (is_404()) {
echo "404";
} elseif (the_subtitle("","", false) == "") {
the_title();
} else {
the_subtitle();
}
?>

Categories