Redirect Query - Header not redirecting with switch - php

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

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

How to localize a language and redirect the user to a specific url with CodeIgniter

I have created a website in two languages with CodeIgniter:
English (en) and
German (de).
The default page is: www.mysite.com
The url for en is: www.mysite.com/en/
The url for de is www.mysite.com/de/
Now I would like to redirect the user according to his/her device language.
I know, the script below it's not the right way.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang){
case 'en':
return redirect(site_url('en/'));
break;
case 'de';
return redirect(site_url('de/'));
break;
default:
return redirect(site_url('en/'));
}
How can I exactly manage it?
I found there is syntax error in your code at case 'de';
It would be case 'de':
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang){
case 'en':
redirect(site_url('en/'));
break;
case 'de':
redirect(site_url('de/'));
break;
default:
redirect(site_url('en/'));
}
And make sure you have load url helper
$this->load->helper('url');
Also remove return from redirect

403 error on passing parameters

I have following function in a php file :
function bc_output($vars) {
switch($_GET['a']){
case 'addip':
bc_add_licenses($vars, trim($_GET['current_ip']));
break;
case 'addiprequest':
if(isset($_POST['submitaddip'])) {
display_guidance_text();
bc_add_licenses_request($vars, $_POST);
} else {
display_guidance_text();
bc_get_licenses($vars, '4');
}
break;
default:
display_guidance_text();
bc_get_licenses($vars);
break;
}
}
Now when i go to url such as http://website.com/addon.php?module=b&a=addip it works fine, although for second case i.e http://website.com/addon.php?module=b&a=addiprequest browser throws 403 error..
Can anyone guide me where to proceed from here, i have echoed etc in the function but always 403 error.
Please comment if you need more info.

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 session variable problem when unset

i have made a function to set a session variable $_SESSION['flash'] in order to store a message between page
function setFlash($string,$type="info") {
switch ($type) {
case "warning":
$_SESSION['flashtype'] = "warning";
break;
case "error":
$_SESSION['flashtype'] = "error";
break;
case "info":
$_SESSION['flashtype'] = "info";
break;
default:
$_SESSION['flashtype'] = "info";
break;
}
$_SESSION['flash'] = $string;
}
and a function to print this message
function printFlash() {
echo $_SESSION['flash'];
unset($_SESSION['flash']);
}
i call this function at the top of every page (naturally after session_start)
the problem is that it doesn't print nothing, but if I comment " unset($_SESSION['flash']);" it prints the message in every page.
how can i solve?
Solved sorry my fault.
my page is something like this
include "func.inc.php"
session start
function editSomething {
that call setFlash()
}
include "template.php" (where printFlash() is called)
now i put printFlash directly in my page and works..bah strange...what's my mistake?
On every page this is what happened:
Make a session
Display flash
Delete flash
Create 'flash' with value
You have to move Create before display.
(it's also not very usefull because you do not transmit 'flash' (it's delete right after been created)

Categories