i have a simple skript that is sending the User to another Page if the input is over 10, from the other skript i want to echo the input. But it giving me the warning that: Warning: Undefined variable $QT
and anyhow it is also copying the whole page that i am including so the pages looking the same.
I tried with require and include and also to paste the whole location of the skript
Skript1:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && $_POST["QT"] > 10) {
$QT = $_POST["QT"];
header("location: ../main/Skript2.php");
die();
}
else{
}
?>
Skript 2:
<?php
include "Skript1.php";
echo $QT;
?>
Both skripts are in the same Folder main.
thanks for helping
You can use header location with a parameter.
Skript 1:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && $_POST["QT"] > 10) {
$QT = $_POST["QT"];
header("location: ../main/Skript2.php/?number=$QT");
die();
}
else{
}
?>
Skript 2:
<?php
$QT = $_GET['number'];
echo $QT;
?>
More than one variables can be joined to a string with a delimiter and then exploded.
Skript 1:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && $_POST["QT"] > 10) {
$xy = "test";
$QT = $_POST["QT"];
$all = $QT.",". $xy;
header("location: ../main/Skript2.php/?number=$all");
die();
}
else{
}
?>
Skript 2:
<?php
$all = $_GET['number'];
$parts = explode(",", $all);
echo $parts[0]."<br>";
echo $parts[1]."<br>";
?>
Related
I am trying to get dynamic $_SESSION[$id] on the second page shown below, but its not working (as per the printout):
First page url
https://example.com/test.php?id=1548393
First page code
<?php
session_start();
$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";
?>
Second page url
https://example.com/test2.php?id=1548393
Second page code
<?php
session_start();
$id = $_GET['id'];
if(isset($_SESSION[$id])){
echo "working";
}else{
echo "not working";
}
?>
i found problem we can not use numeric index for $_SESSION
but we can use number in $_SESSION by convert number to roman numerals
first page url
https://example.com/test.php?id=1548393
first page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
$_SESSION[$roman_id] = "mysecretstringline";
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
second page url
https://example.com/test2.php?id=1548393
second page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
if(isset($_SESSION[$roman_id])){
echo "working";
}else{
echo "not working";
}
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
output
working
thanks #gre_gor and #Katie
Could be that in your normal code (this just looks like a quick mockup), you have a space after ?> somewhere. That could cause issues.
<?php
// start.php
session_start();
$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";
and
<?php
// next.php
session_start();
$id = $_GET['id'];
if (isset($_SESSION[$id])) {
echo "working";
} else {
echo "not working";
}
works for me. Notice no ?> characters.
UPDATE:
The following might be of interest regarding session name constraints (can a php $_SESSION variable have numeric id thus : $_SESSION['1234’])
You have that issue in your example you could just add an id_ and then do the same check when validating/getting the session.
Ok ive asked this question on here today but this code is much easier to follow. i still dont understand why this page would redirect in a loop?
<?php
ini_set('display_errors', 1);
session_start();
$_SESSION['passw'] = "word";
$_POST['passw'] = "word";
if($_POST['passw'] === "word" ){
echo 'post ok';
$_SESSION['passw'] = "word";
unset($_POST);
session_write_close();
header('Location: '.$_SERVER['PHP_SELF']);
}
if($_SESSION['passw'] === "word") {
echo 'password checked ok<br>';
}
?>
Well, let's show just the relevant lines:
$_POST['passw'] = "word";
if($_POST['passw'] === "word" ){
header('Location: '.$_SERVER['PHP_SELF']);
}
Basically you are saying
if (true) {
reloadThePage();
}
Because you are setting the value to "word" immediately before your IF statement.
$_POST['passw'] = "word";
if($_POST['passw'] === "word" ){
So, to fix, remove this:
$_POST['passw'] = "word";
My PHP page can receive the same data from two differents pages, the first one send it using GET, and the second with sessions. How can I make this thing work ?
//$var = empty;
//$_GET['id'] = empty;
//User come from page1.php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$var = $_GET['data'];
}
else {
//User come from page2.php
$var = $_SESSION['data'];
}
Try this:
if (array_key_exists('data', $_GET)) {
$var = $_GET['data'];
} else {
$var = $_SESSION['data'];
}
If you have two pages, page1.php and page2.php and you want to know at first hand which page it is, and what is the ?data= value... then this will do the trick.
<?php
$data = (isset($_GET['data']) && !empty($_GET['data'])) ? $_GET['data'] : '';
if($_SERVER['SCRIPT_NAME'] === 'page1.php'){
$var = $data;
}else if($_SERVER['SCRIPT_NAME'] === 'page2.php'){
$var = $_SESSION['data'];
}
I think I'm doing something fundamentally wrong. Here is my code
index.php
<?php
if(isset($_POST['a'])){
$action = $_POST['a'];
} else if(isset($_GET['a'])){
$action = $_GET['a'];
} else {
$action = "home";
}
if($action == "home"){
$frontImages = glob('assets/images/frontpage/*');
include_once 'home.php';
}
?>
I var_dump($frontImages) inside of homp.php (which btw is showing just fine) but I'm getting that $frontImages is an undefined variable. Both the index.php file and home.php file is in the root folder and here is a image of the path directory:
So not sure what I'm doing wrong here.
use
<?php
if(isset($_REQUEST['a'])) {
$action = $_GET['a'];
} else {
$action = "home";
}
if($action == "home"){
while($acImg = glob('assets/images/frontpage/*')) {
$frontImages[] = $acImg;
}
include_once 'home.php';
}
?>
Now you have a Array but this works by me
I have a function that connects to the database and returns a number and a second function, which returns the size of a file.
If I use the echo function to view the results functions: variable1 = 1345064, and variable2 = 135
,but when I use a comparison, go me incorrect result:
if($variable1 < $variable2)
{
echo 'This code is displayed';
}
else
{
echo 'This code should display';
}
what's wrong?
My source code:
<?php
include 'funkcje.inc';
$login = $_GET['login'];
$fsize = WielkoscPliku2($file);
$tmp = $fsize / 1000000;
$zmienna1 = SprTransfer($login);
$zmienna2 = floor($tmp);
if($zmienna1 < $zmienna2)
{
echo "This code is displayed";
}
else
{
echo "This code should be displayed";
}
?>
SprTransfer function:
<?php
require "connection.php";
connection();
...
function SprTransfer($login)
{
$zapytanie = "SELECT `transfer` FROM `uzytkownicy` WHERE `nick`='$login'";
$idzapytania = mysql_query($zapytanie);
$sprwaznosc = mysql_fetch_row($idzapytania);
return $sprwaznosc[0];
}
?>
Main file:
include 'funkcje.inc';
$login = $_GET['login'];
$fsize = WielkoscPliku2($file);
$tmp = $fsize / 1000000;
$zmienna1 = SprTransfer($login);
$zmienna2 = floor($tmp);
if($zmienna1 < $zmienna2)
{
echo "This code is displayed";
}
else
{
echo "This code should be displayed";
}
function.inc file:
require "connection.php";
connection();
function SprTransfer($login)
{
$zapytanie = "SELECT `transfer` FROM `uzytkownicy` WHERE `nick`='$login'";
$idzapytania = mysql_query($zapytanie);
$sprwaznosc = mysql_fetch_row($idzapytania);
return $sprwaznosc[0];
}
You are using variable names wrong. A variable needs to be preceded by a $.
$variable1 = 4;
$variable2 = 5;
if ($variable1 < $variable2) {
echo 'Yep';
}else {
echo 'Nope';
}
You should look into the basic syntax of PHP namely the Variables Section.
maybe the $ before variables:
if($variable1 < $variable2)
I'm not sure how, but it looks like you are interpreting your numbers as strings.
For example this will display This code is displayed:
<?php
$variable1 = "1345064a";
$variable2 = "135a";
if ($variable1 < $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
// Output: This code is displayed
?>
exmple
Cast your variables to int
<?php
$variable1 = "1345064a";
$variable2 = "135a";
if ((int) $variable1 < (int) $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
// Output: This code should be displayed
?>
example
Debugging:
You can check the type of your variables using gettype(). You should never use the output of gettype() to test for one particular type, since the output string is liable to change from one version of PHP to another.
assuming that missing the $ is just a typo ...
when you run into this type of problem, the best thing is to post a full working code sample. for example, the code below works as it should. it displays "This code should be displayed".
what happens when you run it? what is the difference between this code and yours? answer those two questions and you'll be at least pointed to your answer:
<?php
$variable1 = 1345064;
$variable2 = 135;
if ($variable1 < $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
?>