PHP conditional statetment from form [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
How do I make the statement below work. I have a form that post the values to my function.php. Is it possible for me to include the .html files on the function.php as a content rather than heading to the locations? I am new student learning the hard way.
<?php
if ($_POST['city'] = 'london'){
header('Location: london.html');
}
if ($_POST['city'] = 'manchester'){
header('Location: manchester.html');
}
if ($_POST['city'] = 'conventry'){
header('Location: coventry.php');
}
exit;
?>

You have to use == in if statement, not =.
Try this,
<?php
if ($_POST['city'] == 'london'){
header('Location: london.html');
}
if ($_POST['city'] == 'manchester'){
header('Location: manchester.html');
}
if ($_POST['city'] == 'conventry'){
header('Location: coventry.php');
}
exit;
?>
Check the below link for more info.
https://www.tutorialspoint.com/php/php_decision_making.htm

If you are just using the value in the location as a straight swap of destination - you don't need any ifs at all - just put the variable in the location:
<?php
$city = $_POST['city'];
header('Location:' . $city . '.html');
exit;
?>
//$city = london;
//header('Location:london.html');
If you have different file extensions - eg the .html and .php that you show here - then you can use a switch statement to reduce the if statements:
<?php
$city = $_POST['city'];
switch ($city) {
case "london":
header('Location: london.html');
break;
case "manchester":
header('Location: manchester.html');
break;
case "coventry":
header('Location: coventry.php');
break;
default:
echo "Your destination did not match any of our available cities";
}
exit;
?>

Related

How do I redirect to another page in a condition using php? [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 1 year ago.
I want to redirect to index.html?id=div2 when the if condition is statisfied. And if not, then it should reload the page.
How do I do that?
if($checkAll > 0){
// Redirect to index.html?id=div2
} else{
//Reload page
}
Thanks!
You have to use headers:
if($checkAll > 0){
header("Location: /index.html?id=div2");
} else{
header("Refresh:0");
}
// die();
/* Redirect browser */
header("Location: yourUrl.com");
exit();
it's a good practice to call exit() right after it so that code below it does not get executed.
Try this, I use redirect like this.
function redirect_to($location) {
header("Location: " . $location);
exit;
}
if($checkAll > 0) {
$new_id = mysqli_insert_id($connection);
redirect_to('/index.php?id=' . $new_id);
} else {
redirect_to('/index.php');
}

Redirect in PHP doesnt load my link?

I have 2 php files.
The first one handles the link and stuff. Here is a preview my code:
<?php
session_start();
$link=$_GET['redirect'];
if ($link == '1'){
$_SESSION['link'] = 'https://www.google.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '2'){
$_SESSION['link'] = 'https://www.facebook.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '3'){
$_SESSION['link'] = 'https://www.twitter.com/';
header('Location: selection_handler.php');
exit;
}
?>
This is my selection_handler.php
<?php
session_start();
$link= $_SESSION['link'];
if(isset($_SESSION['user_id']))
{
header("Location: " .$link);
exit;
}
else
{
echo $_SESSION['link'];
}
?>
user_id would be handled later so by default, i would get the echo of the link from the session, right? that works properly but when i try to test setting a value for the user_id, i see my browser trying to load the link. It says 'Resolving host... Waiting for [insert link selected]' but doesnt fully continue to the site but instead im shown a blank page?
UPDATE: I tried changing the header to ('Location: https://www.google.com') instead of getting from the variable but im still getting a blank page

Page Redirection not working after hosting [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
i am trying to redirect a page according to the text value using switch and button click, its working fine in localhost but not working after hosting. please help to overcome this problem.. looking forward to it. thanks.
form coding
<form id="accounForm" class="form-horizontal" method="post" action="redirect.php" >
<INPUT type="hidden" VALUE ="<?php echo( htmlspecialchars( $row['jewellery_name'] ) ); ?>" name="textboxdata" >
<INPUT TYPE = "Submit" class="btn btn-blue" Name = "submit" VALUE = "Add Another">
</form>
redirect.php
<?php
session_start();
ob_start();
$textboxdata = $_POST['textboxdata'];
if (isset($textboxdata)) {
switch ($textboxdata)
{
case COL:
header("Location: new_item_5piececollar.php");
exit();
case BAB:
header("Location: new_item_babybangle.php");
exit();
case BRR:
header("Location: new_item_babyring.php");
exit();
case BAL:
header("Location: new_item_balla.php");
exit();
case BRC:
header("Location: new_item_bracelet.php");
exit();
case BUT:
header("Location: new_item_button.php");
exit();
case CNA:
header("Location: new_item_chain.php");
exit();
case CHK:
header("Location: new_item_choker.php");
exit();
case CHR:
header("Location: new_item_chur.php");
exit();
case CHU:
header("Location: new_item_churi.php");
exit();
case DUL:
header("Location: new_item_dull.php");
exit();
case FBA:
header("Location: new_item_fullballaakshi.php");
exit();
case GRR:
header("Location: new_item_gentsring.php");
exit();
case HNK:
header("Location: new_item_helenecklace.php");
exit();
case SOB:
header("Location: new_item_hsocketbouti.php");
exit();
case JHU:
header("Location: new_item_jhumka.php");
exit();
case KAN:
header("Location: new_item_kaan.php");
exit();
case LRR:
header("Location: new_item_ladiesring.php");
exit();
case LKT:
header("Location: new_item_locket.php");
exit();
case NKL:
header("Location: new_item_longnecklace.php");
exit();
case MAK:
header("Location: new_item_makhri.php");
exit();
case MGS:
header("Location: new_item_mangalsutra.php");
exit();
case MAN:
header("Location: new_item_mantasha.php");
exit();
case NOA:
header("Location: new_item_noah.php");
exit();
case NOT:
header("Location: new_item_noth.php");
exit();
case PDT:
header("Location: new_item_pendent.php");
exit();
case POL:
header("Location: new_item_pola.php");
exit();
case SAC:
header("Location: new_item_sankha.php");
exit();
case TAN:
header("Location: new_item_tana.php");
exit();
case TCK:
header("Location: new_item_tickli.php");
exit();
case TOP:
header("Location: new_item_tops.php");
exit();
case NCK:
header("Location: new_item_entry.php");
exit();
case WRI:
header("Location: new_item_wrislet.php");
exit();
default:
echo "Enter a number please.";
}
}
ob_end_flush();
?>
remove the white space before openning the tag
There are a few minor issues:
You don't need output buffering
Your switch cases are not in quotes so they'll be interpreted as
constants rather than strings
You seem to have some whitespace before the opening <?php
Your redirects aren't relative to the document root so if this in a
sub-directory they may not redirect where you expect them to
Something like this should resolve those issues:
<?php
session_start();
$textboxdata = !empty($_POST['textboxdata']) ? $_POST['textboxdata'] : '';
switch ($textboxdata) {
case 'COL':
header("Location: /new_item_5piececollar.php");
exit();
case 'BAB':
header("Location: /new_item_babybangle.php");
exit();
/* ... and so on ... */
default:
echo "Enter a number please.";
break;
}
As OP requested following my remark, here's a suggestion to improve the code to make it shorter. My PHP is a bit rusty and I've nothing to test here so hopefully there's no syntax error, anyway the idea is there :
<?php
session_start();
ob_start();
$textboxdata = $_POST['textboxdata'];
$URL_array = array('COL' => 'new_item_5piececollar.php', 'BAB' => 'new_item_babybangle.php', 'BRR' => 'new_item_babyring.php');
if (isset($textboxdata) && array_key_exists($textboxdata, $URL_array))
{
header("Location: ".$URL_array[$textboxdata]);
}
else
{
echo "Enter a number please.";
}
ob_end_flush();
?>
Note that for the sake of example I defined $URL_array here but it would be better in some kind of config file, improving the code even more by separating config from logic : if you need to add a new element, you shouldn't modify the code.
Sometimes your output buffers are on if you're on a development server, but in most of the cases it's turned off on live-systems. Check your .INI file for the following parts:
http://php.net/manual/en/outcontrol.configuration.php
Otherwise check out that there is no output before the headers are sent.

PHP Redierct with if, if else and else

Hello I have a question about php.
I want to create a special redirect script but its not working. I want to check if the 'keyword' is in the list. If in the list than redirect with 'header location' If not redirect to a searchmachine with the value you get from $_get.
<?php
$q=$_get['q']
if ($q = tw) {
header('Location: http://twitter.com');
exit;
} else if ($q = fb) {
header('Location: http://fb.com');
exit;
} else {
header('Location: https://searchit.com/search?q='$q'+ ');
}
?>
I have a list with 10 keyword now like
tw twitter.com
fb facebook.com
gg google.com
etc. all in a text list.
The last part of your code: header('Location: https://searchit.com/search?q='$q'+ '); seem to be the issue with the blank pange, also called "white page of death" :)
Try header('Location: https://searchit.com/search?q=' . $q);
You also forgot a semicolon after: $q=$_get['q']
You could also try this setup:
switch($_GET['q'])
{
case 'tw':
header('Location: http://twitter.com');
exit;
case 'fb':
header('Location: http://fb.com');
exit;
default:
header('Location: https://searchit.com/search?q=' . urlencode($_GET['q']));
}
you should write like
$q=$_GET['q'];
and not $_get it would be $_GET['q'] or you can use $_REQUEST['q']
and use
error_reporting(E_ALL);
ini_set('display_errors', true);
to see if any error on you page.

How to correctly check if page was included? [duplicate]

This question already has answers here:
PHP: Check if a file is loaded directly instead of including?
(15 answers)
Closed 8 years ago.
I want to make sure that my pages are being included working index page. I would like to know what would be correct way of assuring that my page is being included instead of rendered by itself?
Right now I'm checking if there are at least 2 included files, but I'm not sure I'd it's behavior.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Included page and how I check is it's included
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
I'm not sure if you're talking about this:
include 'test.php';
If yes, then do a simple test like this:
test.php
$testVar = '1';
index.php
include 'test.php';
echo $testVar;
I have no idea what library you're using, so i hope this simple example will allow you to understand.

Categories