Header - Forwarding variables via PHP to pages without $_GET - php

I have one page A having this code snippet
if (!var) {
header("Location: ".URL, true, 301);
exit();
}
if (!var2) {
header("Location: ". $url2, true, 301);
exit();
}
I want to create a page in between URL/$url2 and A I call this one B.
How do I have to change A to give B the content of URL/$url2 without the user seeing it? I could use something like
Location: mypage.php?url=$url2
But the user could change that what I don't want. If you recommend $_POST how would you do it? If not, what would you do?
I changed it to
session_start();
if (!var) {
//header("Location: ".URL, true, 301);
$data = URL;
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
transition.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="50%,50%">
<frame src="above.php" name="Navigation">
<frame src="http://www.domain.com" name="Daten">
<noframes>
<body>
<p>Something</p>
</body>
</noframes>
</frameset>
</html>
above.php:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
</title>
</head>
<body>
<div style="text-align: right;">Continue
</div>
</body>
</html>
Which leads to
<a href="">

Use a session cookie.
On the page with the info you can save text etc into a session var like this:
$data = 'hello';
$_SESSION['xxx'] = $data;
And get it back on the next page like this:
echo $_SESSION['xxx'];
// Hello
Dont forget you need to run session_start(); before using sessions.

Related

Run php function from index.php

This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example

Add multiple referrer links

I protect page from being access and can only access it by a referrer page, here is my code on landing page
<?php
// request file coming from test referrer
if(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php"))
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Test Landing Page 1</h1>
</body>
</html>
<?php
}
// redirect to redirect.php
else {
header("Location: http://aqsv.com/sites2/testlander/redirect.php");
}
?>
and this is the referrer page
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test Refererrer 1</title>
</head>
<body>
<h1>Test Refererrer 2</h1>
Link me to landing page1
</body>
</html>
this work perfectly on single referrer page only, what i want to do is to have multiple referrer page to access the page , Im new to php and really dont have any idea to do this.. I tried adding http referrer using if else like this
<?php
// request file coming from test referrer
if(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php"));
elseif(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp2.php"))
{
?>
but the second link is not working. Any help would be highly appreciated. Thanks
You can take an array containing all your valid referrer domain. E.g.
<?php
$valid_domains = array(
'domain1',
'domain2',
'domain3'
);
// The checking for valid domain
if ( in_array($_SERVER['HTTP_REFERER'], $valid_domains) )
{
?>
Your HTML goes here....
<?php
}
?>
Please see http://php.net/manual/en/function.in-array.php
Hope the idea will help you.
The syntax if if/elseif... is:
if (something) {
body
} elseif (somethingelse) {
body
}
But you have no body for your if, only for the elseif, so nothing happens in that case.
Since you want the same body for all your tests, you should just use a single if with multiple conditions connected by OR:
if (stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php") ||
stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp2.php")) {
...
}
Another way you can do this is by putting all the allowed referers in an array, and then doing:
if (in_array(strtolower($_SERVER['HTTP_REFERER']), $allowed_referers)) {
...
}
I use strtolower() to make it case-insensitive, like your original tests.

Activate PHP Session when click on a link

right now my php session loads when I load the page. The code looks like this:
<?php
session_start();
$_SESSION['started'] = true;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>First page</title>
</head>
<body>
LINK
</body>
</html>
What I want to do is instead of the PHP session loads when the page loads I want it to load when the user clicks on the link.
<?php
session_start();
if(isset($_GET['link'])) {
$_SESSION['started'] = true;
}
?>
LINK
You could do
<?php
if(isset($_GET['session'])) {
session_start();
$_SESSION['started'] = true;
}
?>
LINK
This reloads the whole page, an AJAX call would be much better

Using sessions to transport a variable over multiple pages

I want to make a transition page, to forward "URL" to. I have this code. Self explanatory. !var means that some var is not given. URL is some url like http://domain.com
session_start();
if (!$some_variable) {
$data = "http://localhost/";
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
transition.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="50%,50%">
<frame src="above.php" name="Navigation">
<frame src="http://www.domain.com" name="Daten">
<noframes>
<body>
<p>Something</p>
</body>
</noframes>
</frameset>
</html>
above.php:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
</title>
</head>
<body>
<div style="text-align: right;">Continue
</div>
</body>
</html>
Which leads to
<a href="">
Why is that so?
It's always best to give full code and not use !var - just put in the correct variable here as it is less confusing. Secondly you don't have any session in above.php
Unknown file:
<?php
session_start();
if (!isset($_SESSION['keks'])) {
//header("Location: ".URL, true, 301);
$_SESSION['keks'] = "http://localhost/";
require("transition.php");
exit();
}
?>
above.php
<?php
session_start();
?>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
</head>
<body>
<div style="text-align: right;">
Continue
</div>
</body>
</html>
This should work:
session_start();
if (!isset($_SESSION['keks'])) {
$data = 'URL';
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
Also in above.php you need
<?php session_start(); ?>
To get the variable.

How to redirect if user already logged in

I have a login script that does this:
$_SESSION['username']=$username;
$_SESSION['password']=$password;
If the user logged in succesfully.
And so I edited the signup page to do this:
<?php
function redirect() {
header(' URL= index.php');
}
?>
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
<?php
if(isset($_SESSION['username'])){
redirect();
}
?>
But it doesn't redirect the user when I logged in with my account that I created. Why is that?
header(' URL= index.php');
should be
header ( 'Location: index.php' );
Also you might want to put a die() statement after the call to header() so that you stop the execution of your script completely.
And you should probably move the call to redirect() above any other output since HTTP headers must be the first thing in the response. It's possible that this is also the cause of your problem.
Change the redirect() function to:
header('Location: index.php');
And move the call to redirect above all the html output:
<?php session_start();
if(isset($_SESSION['username'])) {
redirect();
} ?>
From the header() docs:
Remember that header() must be called
before any actual output is sent,
either by normal HTML tags, blank
lines in a file, or from PHP.
This is what it should look like in the end, taking #Jan's advice to add a call to die():
<?php
function redirect($DoDie = true) {
header('Location: index.php');
if ($DoDie)
die();
}
php session_start();
if(isset($_SESSION['username'])) {
redirect();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
?>
function redirect() {
header('location:index.php');
}
It's header('Location: index.php');
if you want to redirect immediately, then
function redirect(){
header("Location: home.php");
}
if you want to redirect with some delay, then
function redirect(){
header("Refresh: 0;url=default.php");
}
increase the 0 in "Refresh:0" to introduce greater delay.
you might use this to redirect after showing some notification/message to the user.
Note if you are having some trouble in redirecting, then put "exit()" at the end of function
<?php
session_start();
if((isset($_SESSION["username"])))
{
header("Location: home.php");
}
?>

Categories