I have two files. one is header.php and index.php. I didn't understand how $_GET['a'] data is passing from header.php file to index.php for the routing system.
I have tried finding $_GET['a'] passing method from header.php to index.php
Image is a portion of the header.php file
/*index.php*/
include("sources/header.php");
$a = protect($_GET['a']);
switch ($a) {
case "account": include("sources/account.php"); break;
case "login": include("sources/login.php"); break;
case "register": include("sources/register.php"); break;
case "track": include("sources/track.php"); break;
case "testimonials": include("sources/testimonials.php"); break;
case "affiliate": include("sources/affiliate.php"); break;
case "contact": include("sources/contact.php"); break;
case "about": include("sources/about.php"); break;
case "faq": include("sources/faq.php"); break;
case "page": include("sources/page.php"); break;
case "exchange": include("sources/exchange.php"); break;
case "search": include("sources/search.php"); break;
case "password": include("sources/password.php"); break;
case "email-verify": include("sources/email-verify.php"); break;
case "logout":
unset($_SESSION['bit_uid']);
unset($_COOKIE['bitexchanger_uid']);
setcookie("bitexchanger_uid", "", time() - (86400 * 30), '/'); // 86400 = 1 day
session_unset();
session_destroy();
header("Location: $settings[url]");
break;
default: include("sources/homepage.php");
}
I expect to know how $_GET['a'] is passing from header.php to index.php
$_GET query contains the keys/values array that are passed to your script in the URL.
If you have the following URL:
http://www.example.com/test.php?a=login
Then $_GET will contain :
array
'a' => string 'login' (length=5)
$_GET is not read-only, you could also set some values from your PHP code, if needed :
You can pass data to $_GET in your header.php
$_GET['a'] = 'register';
But this doesn't seem like good practice, as $_GET is supposed to contain data from the URL requested by the client.
In header.php file you need change urls
Link
Source
Related
I am working on a , project which i have configured to serves multiple pages from one index.php file using a switch statement like this:
switch(isset($_GET['q']{
case 'page':
require 'link_to_page.php';
break;
case 'login':
require = 'link_to_login.php';
break;
default:
require = 'link_to_404.php';
break;
}
As time goes by and more pages are added, i decided to move it to a selectPage() function which i now called and assigned to a variable $page and required it in my index.php file to make things simpler like this:
myFunctions.php
selectPage()
{
switch(isset($_GET['q']{
case 'page':
$output = 'link_to_page.php';
break;
case 'login':
$output = 'link_to_login.php';
break;
default:
$output = 'link_to_404.php';
break;
return $output;
}
}
My index.php looks like this:
require 'myFunctions.php';
$page = selectPage();
require $page;
Now the problem here is, regardless of which case is true case page: or case 'login':, $output returned is always equal to the first line of case condition checked, for example when case page: is the first line of case statement and $_GET['q'] == 'login', case page: $output value is returned, when i swapped the case 'login': with case page: for it to be the first condition checked, case 'login': $output value which is now the first line of condition is returned even if $_GET['q'] == 'page'.
i have also tried it with an if(statement) and the same thing happened.
How do i fix this, is there something am doing wrong?
Syntax error your Switch statement.
Right SYNTAX :
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
So, myFunctions.php Page
function selectPage()
{
$page = isset($_GET['q']) ? $_GET['q'] : null;
switch ($page) {
case "page":
return 'link_to_page.php';
break;
case "login":
return "link_to_login.php";
break;
default:
return "link_to_404.php";
}
}
index.php page
require 'myFunctions.php';
$page = selectPage();
require $page;
Try this:
switch($_GET['q']){
....
}
You try to check $_GET['q'] but really you check isset($_GET['q']), so when PHP gets true it tries to compare values you give in case statement with true. And as your value are not empty or false values, that condition is true, and code under it will execute.
I want to send traffic to my website, into this link:
mydomain.com/?foo=1&bar=1
This page should have a code that checks for the bar parameter, and modifies the content of the page according their value, for example:
if(isset($_GET['bar']));
switch ($_GET['bar']) {
case "1":
$var = "a";
break;
case "2":
$var = "b";
break;
default:
$var = "c";
}
<?php echo $var; ?>
However, after this content modification, I want the URL address line of the browser, to show only this URL:
mydomain.com/?foo=1
The reason is this:
After reaching my page, I want to give the visitor an option to visit another third party website. I want the third party to know which page the visitor came from, but I don't want the third party to know about the existence of the bar parameter.
session_start();
if(isset($_GET['bar'])){
$_SESSION['bar']=$_GET['bar'];
header("Location: mydomain.php?foo=".$_GET['foo']);
}
then
session_start();
switch ($_SESSION['bar']) {
case "1":
$var = "a";
break;
case "2":
$var = "b";
break;
default:
$var = "c";
}
echo $var;
I want to change the language without showing the url:
http://myweb.com/?lan=AL
or
http://myweb.com/about-us?lan=AL
How can it be done in background, not to show in url.
This is the code below.
require('_inc_lang/lan_en.php');
require('_inc_lang/lan_al.php');
require('_inc_lang/lan_de.php');
if(!isset($_SESSION['lan'])){
session_start();
}
if(isset($_GET['lan'])){
$_SESSION['lan'] = $_GET['lan'];
}
$lan = isset($_SESSION['lan']) ? $_SESSION['lan'] : 'al';
switch ($lan) {
case 'al':
$TEXT = $TEXT_AL;
break;
case 'de':
$TEXT = $TEXT_DE;
break;
case 'en':
$TEXT = $TEXT_EN;
break;
}
You can do it based on the browser language
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "de":
$TEXT = $TEXT_DE;
break;
case "en":
$TEXT = $TEXT_EN;
break;
case "al":
$TEXT = $TEXT_AL;
break;
default:
$TEXT = $TEXT_EN;
}
?>
Change from GET to POST.
<form method="post">
..button/select/whatever have you
</form>
if(isset($_POST) && /* sanitise */)
$_SESSION['lan'] = $_POST['lan'];
Code needs a tidy up but you can do that yourself :) Is this what you are looking for?
Edit:
Absolutely totally must use a link or you will explode? The following SO pages will magically show you how!
Use a normal link to submit a form
How to submit a form with JavaScript by clicking a link?
You're already copying the language choice to the session, so if it is found in the URL, just location: to the version without the URL:
if(isset($_GET['lan'])){
$_SESSION['lan'] = $_GET['lan'];
header("Location: ".$_SERVER['SCRIPT_URI']);
}
Edit: note that I added variable $_SERVER['SCRIPT_URI'] instead of hard coded location; now it will work regardless of where it is called (SCRIPT_URI will give you server/page without query string)
I use this example below to ask how to pass an array into a Switch instead of listing them all out like this?
Perhaps if I had an array being fetched from a database for all the 'pages' as page.
I figured this method wasn't the most elegant approach.
switch($_GET['page'])
{
case 'home':
include('home.php');
break;
case 'oem-customers':
include('oem-customers.php');
break;
case 'job-shop':
include('job-shop.php');
break;
case 'anodized-magnet-coils':
include('anodized-magnet-coils.php');
break;
case 'design':
include('design.php');
break;
case 'services':
include('services.php');
break;
case 'black-foil':
include('black-foil.php');
break;
case 'contact':
include('contact.php');
break;
case 'order';
include('order.php');
break;
default:
include('home.php');
}
How about
switch($_GET['page'])
{
case 'home':
case 'oem-customers':
case 'job-shop':
case 'anodized-magnet-coils':
case 'design':
case 'services':
case 'black-foil':
case 'contact':
case 'order';
include("$_GET[page].php");
break;
default:
include('home.php');
}
You can create a mapping:
$mapping = array(
'home' => 'home',
'oem-customers' => 'oem-customers',
#....
);
$page = $_GET['page'];
$file = isset($mapping[$page]) ? $mapping[$page] : 'home';
include($file.'.php');
Or first look up the file, check if the file exists, and perhaps use extra mapping if needed. Note: Sanitize the input, users could send ../../etc/whatever in the request.
Example with an array :
$pages = array(
'oem-customers',
'job-shop',
'anodized-magnet-coils',
'design',
'services',
'black-foil',
'contact',
'order'
);
if(in_array($_GET['page'], $pages)){
include($_GET['page'].'.php');
}else{
include('home.php');
}
You could do the following:
switch($_GET['page']){
case 'home':case 'oem-customers':case 'job-shop':case 'anodized-magnet-coils':
case 'black-foil':case 'services':case 'design':case 'contact':case 'order':
include($_GET['page'].'.php');
break;
default:
include('home.php');
}
well, you seem to have a pretty good match between $_GET['page'] and the filename of the page; an easy thing to do is:
$toInclude = "{$_GET['page']}.php";
include($toInclude);
of course, you want to be careful; someone could trick you by passing something bad in page, like '../../some_other_project/delete_all_data.php'. so can keep a validation list of all of the pages you allow:
$validPages = array('order.php', 'home.php');
if (!in_array($toInclude, $validPages))
$toInclude = 'home.php';
If it were me I'd move your scripts that can be requested into a requests/ folder and do the following:
$script = basename( $_GET['page'] );
$dir = 'requests/';
$file = $dir . $script . '.php';
if ( !file_exists( $file ) ) {
$file = $dir . 'home.php';
}
include( $file );
Much more compact than anything else.
Will auto update whenever you add a new page.
Built-in check for existence of file before include.
Safe due to the containing requests/ folder and basename().
I would suggest a total different way:
$base_path = '/path/to/dir/';
$req = basename($_GET['page'] . '.php');
$page = $base_path . $req;
$file = file_exists($page) ? $page : $base_path . 'home.php';
include($file);
I’m working on a site and I kept getting a
Notice: Undefined index: id in file root on line 3
I know its something simple but can’t figure out where the problem lies exactly.
here is the code:
<?php
switch($_GET['id']) {
default:
include('pages/hello.php');
break;
case "testimonials":
include('pages/testimonials.php');
break;
case "faq":
include('pages/faq.php');
break;
case "raq":
include('pages/raq.php');
break;
case "contact":
include('pages/contact.php');
break;
}
?>
line 3 would be <?php switch($_GET['id']) {
any help would be greatly appreciated!
This is because in your url id=x is not always set, so when your trying to switc hthe value its not there. what you should do is like so:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : ''; //This is just a short if-else
switch($id)
{
default:
include('pages/hello.php');
break;
case "testimonials":
include('pages/testimonials.php');
break;
case "faq":
include('pages/faq.php');
break;
case "raq":
include('pages/raq.php');
break;
case "contact":
include('pages/contact.php');
break;
}
?>
Basciall $id = isset($_GET['id']) ? $_GET['id'] : ''; what this says is, IF id is within the url then use that, otherwise use an empty string, the reason for the empty string is it will trigger the default: within the switch statement
this way $id will always be set to something.
Make sure that id is set and the default should go at the end:
if (!isset($_GET['id'])) {
include('pages/hello.php');
}
else{
switch($_GET['id']) {
case "testimonials": include('pages/testimonials.php'); break;
case "faq": include('pages/faq.php'); break;
case "raq": include('pages/raq.php'); break;
case "contact": include('pages/contact.php'); break;
default: include('pages/hello.php'); break;
}
}
For security reasons, make sure to sanitalize yor $_GET['id']. I would suggest you to setup an array of allowed pages and include those that are in the array. You can use in_array function for that.
It seems that the url parameter 'id' is not defined, that's the cause of the notice.
You should first check for its existence, eg.
if (isset($_GET['id'])) {
your code here
}