Not able to GET parameters functioning in include_once - php

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');

Related

PHP error notices. Should they never be ignored?

I'm researching how to better organize my website content with PHP, and I had a question regarding unimportant error notices.
<?php
switch ($_GET['filename']) {
case 'home':
require('src/home.php');
break;
case 'quiz';
require('src/quiz.php');
break;
default:
if ($_GET['filename'] == '') {
include('src/home.php');
} else {
header('HTTP/1.0 404 Not Found');
include('src/page_not_found.php');
}
break;
}
?>
For example here; it's obviously telling me that it's getting undefined when I try to get the filename in the URL parameter. In this context, it's empty, and I'm doing this on purpose to check if there's something in there and if it should be interpreted as one of my other files.
I'm aware that you can add "error_reporting(E_ERROR | E_PARSE);" at the start of the line to hide the notice and the website will work just fine like that, but I was wondering if this is something I should always "fix"?
I was thinking of doing an if condition before the switch case:
if ($_GET['filename'] == ""){
include('src/home.php');
}
But that will throw me a notice as well, since what I am checking is undefined and will trigger the error notice regardless. What should I do?
Tim Lewis answered my question, thank you by the way!; Instead of hoping that a file is there, you can instead use isset().
So, instead of what I made, I would do something like this, to first check if the content is set before doing anything else:
<?php
if (!isset($_GET['filename'])){
include('src/home.php');
} else {
switch ($_GET['filename']) {
case 'home':
require('src/home.php');
break;
case 'quiz';
require('src/quiz.php');
break;
default:
header('HTTP/1.0 404 Not Found');
include('src/page_not_found.php');
break;
}
}
?>

Access all files from separate folders

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.

Check result of PHP include

I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.

1. Using switches for multiple url variables

ok say i have these pages,
/admin/admin.php
/admin/blogger.php
inside my main index.php, i have a switch array that runs an 'if exists $page' function, which works fine. all my pages are called from site.com/index.php?page=about, site.com/index.php?page=misc, etc etc.
inside my admin.php page is a php tabbed navigation that uses 4 tabs for my admin sections (blogger/image manager/file manager/quotes manager). it uses a switch of tabfunctions for the 4 pages.
the blogger.php is where i have my switch in question.
now for my question:
instead of having multiple pages for the blog system, "delete.php, add.php, edit.php, etc" and using like 'to delete click (delete.php?id=1) here', i wanted to try and run it all from the blogger page. for example, "site.com/admin.php?page=blogger&act=dp/ep/ap" would get whichever $act is being passed and then using a switch to complete the action.
my PAGE switch works fine, but when i try to call more switches, it doesn't work. I tried using this as my code
if(isset($_SESSION['id'])) {
$act = $_GET['act'];
switch ($act) {
case 'ap':
addPost();
break;
case 'ep':
editPost();
break;
case 'dp':
delPost();
break;
default:
~table setup
~$query, $result
~if / while loops
~echo $row->article_id/title/author/date
echo "Edit"
...
...
...
here is the issue i'm having. the page is correct, the tabindex is correct, then it stops working. i just get a blank page, not the edit page like i should. my editPost function is correct, as i've tested it from the editpost.php?id=1 way, which i am trying to avoid. and yes, my functions are included from here as well.
is the url not being passed right? or is my act switch not set up correctly. maybe setting an isset($_GET['act']) before the switch? i'm at a loss.
thank you.
Do you want to pass multiple actions at once like this?
//test.php?action=add/update/notify
if(isset($_GET['action'])) {
$act = $_GET['action'];
//split actions
$actions = explode('/', $_GET['action']);
foreach( $actions as $action ){
switch ($action) {
case 'add':
echo "add<br />\n";
break;
case 'update':
echo "update<br />\n";
break;
case 'notify':
echo "notify<br />\n";
break;
default:
// default action if no match (runs for every item of $actions array)
break;
}
}
}
site.com/admin.php?page=blogger&act=dp/ep/ap
You can't do that. Specifically act=dp/ep/ap. I would recommend a mix of mod_rewrite and multiple $_GET so then you could do like what WordPress does:
site.com/admin/blog/edit
Mod_rewritten to:
site.com/admin.php?page=blog&act=edit
From there you just have to use $_GET and have just one switch with dependencies.
$page = $_GET['page'];
$action = $_GET['act'];
switch ($page) {
case 'blog':
do($action);
break;
case 'news':
do($action);
break;
case 'users':
do($action);
break;
default: echo 'Try again.';
break;
}
function do($act) {
switch($act) {
case 'delete': confirmDelete();
break;
case 'update': updateConfirm();
break;
}
}
You get the idea.

php var isset, ?data=menu

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.

Categories