1. Using switches for multiple url variables - php

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.

Related

Redirect Query - Header not redirecting with switch

I'm trying to redirect my links, using a php file but when I click the links nothing happens or I get a 500 error.
I've tried redirecting websites such as https://test1.org, https://test2.org and https://test3.org using a php file with switch.
<?php
$page = $_GET['page'];
switch($_GET['page']) {
case '1':
header('https://test1.org');
break;
case '2':
header('https://test2.org');
break;
case '3':
header('https://test3.org');
break;
default:
echo "Hello";
}
Anyone know whats wrong?
I'm wanting it to redirect when someone clicks the link, example redirect.php?page=1, redirect.php?page=2 or redirect.php?page=3...etc
Use "Location:" in your header() function call to redirect the page to the specified URL, like so:
header('Location: https://test1.org');

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;
}
}
?>

PHP Action File how to

I am working on a project, I want to know if I am doing this correctly.
I have different forms in my project/page and I redirect them to one php file (action.php)
Every form sends its own id so the action.php file knows what to do
Little example:
if($_POST['action'] == "sendmail") {
//Send mail actions
}
if($_POST['action'] == "deleteuser") {
//Mysql query for deleting the user
}
Like that.
So my question is are there better ways to do this like with a function tag or something like that?
I usually do this with switch/case statements:
switch ($_POST['action']) {
case 'sendmail':
// send mail actions
break;
case 'deleteuser': {
// delete user actions
break;
...
default: {
// Report unrecognized action
}
You could further organize this by having the actual action code in separate functions, so it might look like:
case 'sendmail':
sendmail();
break;

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.

Not able to GET parameters functioning in include_once

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

Categories