Add multiple referrer links - php

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.

Related

$_GET in PHP not getting utm values

I am trying to print / echo utm_source, utm_medium and utm_campaign query values using this PHP code;
echo $_GET['utm_source'];
But strangely for some unknown reason on my server its not printing the values, when I replace utm_source to something else like test_source I am able to see the value in print.
Never faced any such issue, can anyone guide me here.
<!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>
<?php echo $_GET['utm_source']; ?>
</body>
</html>
If your site is using WPengine, you will not be able to get the utm variables (and any additional variables after the utm's) out of the URL using PHP. Some web hosts specifically target and remove these utm parameters from server requests for performance reasons. Tricky part is, utm variables are still going remain in the URL which means that you can retrieve them using Javascript instead if you need to.
Source: WPengine article
https://wpengine.com/support/utm-gclid-variables-caching/
Your original question quoted this code
<!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>
<?php $_GET['utm_source']; ?>
</body>
</html>
If you add an echo to this statement it will work
<?php echo $_GET['utm_source']; ?>
when tested using
test.com/test.php?utm_source=1
A safer piece of PHP code would be
<?php
if ( isset($_GET['utm_source']) ) {
echo $_GET['utm_source'];
} else {
echo 'the parameter utm_source is missing';
}
?>
I know it's late. but maybe this answer helps another developer.
// get the value of outlink, source, campaign, medium, content
let url = new URL(window.location.href);
let outlink = url.searchParams.get('outlink'),
utmSource = url.searchParams.get('utm_source'),
utmCampaign = url.searchParams.get('utm_campaign'),
utmMedium = url.searchParams.get('utm_medium'),
utmContent = url.searchParams.get('utm_content');
console.table( { outlink , utmSource, utmCampaign,
utmMedium,utmContent});

set page title and meta tags from another page in dreamweaver

I'm developing a php web application using Dreamweaver CS5. When I create a new page, Dreamweaver automatically adds the following code
<!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>
</body>
</html>
What I'm trying to do is to take this code and put it in a file, say 'head.php', and then I want to import this head.php file to all the pages I create using
<?php include("head.php"); ?>
If I do this, it means that I'm gonna have the same page title for every page because the code <title>Untitled Document</title> is included in head.php.
So is there a way for me to send the page title via a variable from the new pages I create and then set it on the head.php document. So if I had a customer.php page, it would look something like this.
$pageTitle = "Customer List";
<?php include("head.php"); ?>
Then pass $pageTitle to the the head.php
Inside your header.php
function header($title)
{
echo '<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.$title.</title>
</head>';
}
then you include your head.php
< ?php include("head.php"); ?>
and finally, instead the < head >< /head > tags in your main file your call the function to create the title.
<?php header('This is my Awesome Page'); ?>
Or still
<?php
$pageTitle = "My Title";
header($pageTitle);
?>
This should work for you.

PHP redirection returns "Cannot modify header information"

I currently have a very simple page that redirects to another URL. However for some reason, I cannot get this to work.
I'm sure there is a very simple solution to this problem and any help to make me understand exactly what's going on would be appreciated.
Here is my page:
<!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>Test - Youtube</title>
</head>
<body>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
</body>
</html>
Thanks
You are having some Outputs on your page before sending header and that's why you can not redirect, you should use output buffering like this:
<?php
ob_start();
?>
<!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>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs');
die();
?>
</body>
</html>
<?php
ob_end_flush();
?>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
remove all the html except above php code if it's only a redirect page.
This is how the code should look like:
<?php
ob_start();
?>
<!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>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs'); // Are you sure that's the right
address?
?>
</body>
</html>
<?php
ob_end_flush();
?>
Anyway, the page is redirecting to another page, why do you need the html tags?
You should remove all the page content except PHP script:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Or
put your PHP script on top of the page:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Make sure there is no empty space before php tag above.
<!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>Test - Youtube</title>
</head>
<body>
</body>
</html>
As redirect sends headers to the browser it MUST be sent before any output.
You can use the method below to redirect using a different method depending on whether the headers have been sent:
<?php
function redirect($filename) {
if ( ! headers_sent())
header('Location: '.$filename);
exit; // just a good practice to EXIT after redirecting
else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$filename.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
echo '</noscript>';
}
}
redirect('http://www.google.com');
?>
This will fallback to using javascript if the headers have already been sent.
If you want to use the header() method it must be at the top of your code before any other output (including whitespace).

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

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