How can I access all the files from a folder for example:
I added my site files to a folder. In the root of the website, I have the folder that consist of my files, index.php and my theme menu. So my question is,
How can I access my files in that folder from a specific link in the menu?
I don't want them to be accessed like "myste.com/sections/forum.php" rather I need to know how to access them like "mysite.com?page=forum" or something like a CMS. I really appreciate if someone can help me with this. I havE been searching for solution but no luck.
Thanks.
The command mysite.com?page=forum will run the index.php file in the mysite root folder.
So you need to write some code in the index.php file to redirect to the correct place, like
<?php
if (isset($_GET, $_GET['page'])) {
// sanitize the $_GET contents
switch ($_GET[['page']) {
case 'form' :
header( 'Location: sections/forum.php' );
exit;
break;
case '...' :
// etc etc
}
} else {
echo 'No $_GET';
}
Unfortunately it does not stop there, as its likely you will want to put other parameters on the querystring as well as the page. So now you have to decide what to do with those other parameters. Do you add them to the header() or do you store them somewhere else and make sure the rest of you app knows where to get them from.
<?php
if (isset($_GET, $_GET['page'])) {
// sanitize the $_GET contents
$gets = $_GET;
unset($gets['page'];
$qs = '?' . implode('&',$gets);
switch ($_GET[['page']) {
case 'form' :
header( 'Location: sections/forum.php' . $qs );
exit;
break;
case '...' :
// etc etc
}
} else {
echo 'No $_GET';
}
You have a lot of ways to do this, but if i understood your question, one solution could be:
if (!empty($_GET["page"])) {
switch ($_GET["page"]) {
case "forum":
include('section/forum.php');
break;
case "something_else":
//include other file, or do whatever want
break;
default:
//every else case
break;
}
}
Thanks both, yea kinda work's but in the browser instead of showing mysite.com/index.php?page=forum shows mysite.com/sections/forum.php
Did I missed something ?
Thanks.
Related
Hi guys I'm trying to do a strange thing, I want put all of my code, of all my website pages inside an if, for example I want do this:
<?php
include('..../...../php/secure-open.php');
if(......){
ALL OF WEBSITE CODE HERE
include('..../...../php/secure-close.php');
}
Where secure-open.php is:
<?php
if(security check here){
?>
And secure-close.php is
<?php
}else{
die();
}
?>
So all the will will be executed only if the security condition is true, but when I try to do it, it says, Unexpected document ends on line`....
Looks like you have missed closing bracket :)
You really should not resolve your security checks that way, but if you really sure that's what you need, move "ALL OF WEBSITE CODE" to different file and just include it inside that if.
if(security_check) {
include "website.php";
} else {
die();
}
And one more easy-to-implement fast check:
index.php:
if (security_check) {
define('SECURE', true);
}
require "website.php";
website.php:
if (!defined('SECURE')) {
die();
}
I'm developing a system where users need to make an account, and once the account is fully created it should take them to the home page, for this I use the PHP tag header.
My code looks like this:
private function redirect($location) {
if(!isset($location)) { $location = '/'; }
header('Location: ' . $location);
}
For some reason when I execute this without specifying $location or with anything it's taking me to (in the url bar)
/var/www/html/our-site-name/<$location>
Does anyone know what might be the issue here?
say you have a directory structure like this
<wwwroot>
<login>
<signup>
index.php
then your function should look like this if it is run from the signup folder
private function redirect($location) {
if(!isset($location)) { $location = './index.php'; }
header('Location:'.$location);
}
"./" takes you up 1 directory
Ok, am using traditional php, no frameworks, nothing, I am using simple procedural way, now my question is I was searching for a while but am not getting an answer to my question, I am not using .htaccess files as of now, but I really need to understand how 404 error works? I am having a website, where I show post's related to category, say category=php, so I pass this as a get request
$_GET['category'] == 'php';
Now currently what am doing is something like this :
$pocategory = $_GET['category'];
if($pocategory == 'php' || $pocategory == 'javascript') {
//Then show related posts
} else {
header('Location:404.php');
exit;
}
I mean I just want php and javascript as valid request's value, rest I want to redirect to 404 but am not understanding how to do it so I did this way, what if am having more than 50 categories? I cant list them all in this if condition, Inshort how to detect whether the given get request value is invalid or not..
Any help will be much appreciated.
.htaccess is the way to do this.
ErrorDocument 404 index.php?404
that line will tell apache what file to load. The example above calls the main index.php script.
add something like this to the top of your index.php file:
$error_404 = isset($_GET["404"]) ? true : false;
now you can detect if you have a 404 error request. $error_404 will be true, so why not add a simple function:
function error_404($error_404)
{
if($error_404 == true)
{
// do some error stuff here, like set headers, and some text to tell your visitor
}
}
now just call your function:
error_404($error_404);
best to do that immidiatley after the get handler:
error_404($error_404)
$error_404 = isset($_GET["404"]) ? true : false;
or combine the two into one line:
error_404($error_404 = isset($_GET["404"]) ? true : false);
to address the question, add this to the relevant script:
$pocategorys_ar = array("php","javascript");
if (!in_array($pocategory, $pocategorys_ar))
{
error_404(true);
}
Make sure it has access to the error_404() function.
You could put all categories inside an array like this:
$pocategories = array
(
'php',
'javascript'
);
if (in_array($pocategory, $pages))
{
// ...
}
else
{
header('Location:404.php');
}
Another thing you could do is creating a html/php file for every category and do it like so
if (is_file('sites/' . $popcategory . '.php')
{
include('sites/' . $popcategory . '.php');
}
else
{
header('Location:404.php');
}
I have script where some url leads to index.php?data and the page opens
how can I use that to use the url open an "subpage" like index.php?data=menu or so?
the code I use with the first one is
if(isset($_GET['palvelut'])){ echo "this is a sample"; }
You have the right idea, but allow me to elaborate at little on this. You need to check if $_GET['data'] is set, by doing isset($_GET['data']), and if that is set, checking to see if it has a given value, "menu" in this case. You can do that like this $_GET['data'] == "menu". Putting it all together, you get this:
if (isset($_GET['data']) && $_GET['data'] == "menu") {
/* Menu code goes here */
}
If you would like to have this work for multiple values for data you can do the following:
if (isset($_GET['data'])) {
switch($_GET['data']) {
case "Possible_Value_1" :
/* Code for this condition appears here */
break;
case "Possible_Value_2" :
/* Code for this condition appears here */
break;
/* etc... */
default :
//Just as a precaution...
echo "Invalid 'data' value supplied!";
break;
}
}
Hope that helps.
If I understand your question (which I don't), you've answered it already.
Your URL is: http://www.mydomain.com/index.php?data=something
Your code would be:
if (!isset($_GET['data'])) {
//do something because no data argument was passed
} else {
switch ($_GET['data']) {
case "homepage":
header("location: homepage.php");
die;
break;
case "someotherpage":
header("location: someotherpage.php");
die;
break;
//and so on
}
}
Obviously instead of using a header redirect, you might just require() or include() a file, or do something else entirely.
when i call include once without any GET parameters it works but with setting GET parameters on trackinglogs.php nothing happens please suggest me what do to..
my php code is: firstfile.php
include_once('trackinglogs.php?todo=setcookie');
?>
my second file is trackinglogs.php
<?php
$action=$_GET['todo'];
switch($action)
{
case "setcookie":
echo "hi";die();
break;
default:
echo "error"; die();
break;
}
?>
thanks for you precious time
You cannot pass parameters when including like that, include does not make an HTTP request.
The most minimal solution, although I do not recommend it, is to simply set the parameters yourself so that trackinglogs.php finds them:
$_GET['todo'] = 'setcookie';
include_once('trackinglogs.php');
A much better solution would be to put the code that tracks logs inside a function, and call that providing this operating parameters at the same time. So you 'd have something like:
<?php
function track($action) {
switch($action) {
case "setcookie":
echo "hi";die();
break;
default:
echo "error"; die();
break;
}
And you would do:
include_once('trackinglogs.php');
track('setcookie');