I am a complete PHP newbie, and I'm not even sure if I should be using PHP for what I'm doing, but here goes. Basically all I want to do is based on where a user comes from, change a link on the page to link to another location. I feel like this is very basic, but I'm not sure exactly how to phrase my search to get the best results. How would I do this?
You probably want something along the lines of
<?php if ($_SERVER['HTTP_REFERER'] === 'http://www.example.com') { ?>
1
<?php } else { ?>
2
<?php } ?>
$_SERVER['HTTP_REFERER'];
This will give you the url of client requesting the page. As said in this post: "Note that it is provided by the client so it may be empty or faked, so don't trust it security-wise."
source of REQUEST
Not sure exactly how you would best google that, but hopefully this will get you started:
To figure out where a user came from, you need $_SERVER['HTTP_REFERER'].
Here's a tutorial based on doing a header redirect on that: http://bytes.com/topic/php/answers/7406-how-redirect-based-_server-http_referer
But you'll want to substitute echoing out a link instead of using header().
So quick snippet it would be something like this:
if (!empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'stackoverflow.com')) {
echo "<a href='http://thatplaceiwanttogoto.com'>Here</a>";
} else {
echo "<a href='http://thatotherplace.com'>There</a>";
}
Related
First time posting a question here. I'm definitely NOT a coding expert, I have learned some HTML and CSS thanks to Google, StackOverflow, and lots of trial and error, but this one seems to be a PHP issue, and I know almost nothing about PHP.
I am creating a subscription Wordpress website and I need to create an iframe with a dynamic src value.
The first part of the src value would be the same for every user (for example https://app.example.com/autoin/) and then it should be completed by a unique code given to each user. That code lives in one of the fields in the user's profile. I'm currently using the second address line for that. The id or name for that field is address-two.
The closest I have come across to a working code is the one below, which I add to directly to the page I will use, using a Raw HTML box from WPBakery.
<?php
$current_user = wp_get_current_user();
if($current_user) {
?>
<iframe class="metricool-iframe" src="https://app.example.com/autoin/<?php echo $current_user->address-two; ?>>
</iframe>
<?php
}
?>
When I check the View Page Source in Chrome, I can see that the URL is still https://app.example.com/autoin/<?php echo $current_user->address-two; ?>
So, it's not really working.
Please, is there anything I can do to improve the code above. Or is there any other method? Please keep in mind that I'm a total beginner to this :P
Thank you in advance!!
Ern.
You could try this:
<?php
$current_user = wp_get_current_user();
if(isset($current_user)) {
$useraddress = $current_user->address-two;
echo "<iframe class='metricool-iframe' src='https://app.example.com/autoin/".$useraddress."'></iframe>";
} else {
echo "No User Set!";
?>
If this helps please mark as answer, it would mean a lot!
EDIT:
What is the name of your file? (Ex: index.html, index.php)
And what webserver are you using, and are you 100% sure it has php running on it?
It's a Wordpress website, the index page is index.php.
I checked my server with https://iplocation.io/ and it returned "Apache". Not sure if that is what you were referring to.
I was wondering if, instead of generating the URL with PHP, I just put the full unique URL in the address-two profile field?
I tried this, but it doesn't work:
<?php
$current_user = wp_get_current_user();
if(isset($current_user)) {
$useraddress = $current_user->address-two;
$useremail = $current_user->email;
echo "<iframe class='my-iframe' src='.$useraddress."' email='.$useremail."'></iframe>";
} else {
echo "No User Set!";
?>
Also, note that I will need to request the user's email address too and add it as a parameter.
Thank you very much!
So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.
What I am try to do is use $_SESSION['user_id'] to check if 'user_id' is = to "number", say 56 for example, if so load page, if not redirect user to "billing/".$_SESSION['user_id'].".php";
So far I have this
<?php
if ($_SESSION['user_id']) === 56) {
//do nothing
} else {
header("Location: billing/".$_SESSION['user_id'].".php");
exit();
}
?>
I know this code is wrong but hopefully it conveys what I am trying to accomplish.
Thanks in advance for your help and code snippets.
do not edit the code in your question based on the answers. You are making it impossible to understand what are you talking about.
If you want to add something - ADD it below the original text.
Ask clear, certain question. Describe the problem you face and what kind of solution you need.
Separate matters. As a matter of fact, sessions has nothing to do with redirects. If you want to know how to use sessions - ask how to use sessions. If you already have valid and verified session variable but have no idea of redirects - ask about redirects. If you don't know how to compare values - ask it. If you know everything but not certain about some bells and whistles of the code styling - ask this particular question.
Now, what is your question?
You're pretty close, but you could use the php function http_redirect instead for the redirect, to be more concise (requires the PECL library):
<?php
if ($_SESSION['user_id'] == 56) {
//do nothing
} else {
http_redirect("billing/".$_SESSION['user_id'].".php", array(), true, HTTP_REDIRECT_PERM);
}
?>
The PHP Doc for header() gives a lot of info about how to use the header function. The most important is:
Make sure no html or text has been echoed or sent to the browser before you call header().
You need an exit(); after header() since you're done rendering the page and some browsers prefer it.
HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path so you'll want to put the full URL after 'Location:'. See php doc for example.
Why not check the opposite? And for readability it is wise to use {} not "." (as long as your editor highlights this)
<?php
if($_SESSION["user_id"] != 56) {
header("location: billing/{$_SESSION['user_id']}.php");
die();
}
?>
I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.
So I am trying to get the page where a visitor came from. I inserted this code into a php file and I am trying to see the page's URL but it is not working, any suggestions?
<?php
$ref = getenv("HTTP_REFERER");
echo $ref;
?>
(added this after some answers)
I have also tried
print $_SERVER["HTTP_REFERER"];
and that doesn't work either
it worked after i updated the website many times, not sure why was there a problem in the first place, thanks anyway :)
Have you tried accessing through the $_SERVER superglobal?
print $_SERVER["HTTP_REFERER"];
$_SERVER['HTTP_REFERER'] is the best way to access this information.
Based on your comments on other responses:
Are you actually coming from somewhere? If you refresh your browser this value will likely not be sent. So make sure your browser is sending the header. If you put this script on a public url, I'll be happy to check it out and verify.
You should really turn on all errors. If the header is not sent and you access it anyway, PHP will emit an E_NOTICE. If you're debugging your code you should turn on all error message and make sure there are no E_NOTICE's or worse.
Maybe a stupid remark, but $_SERVER["HTTP_REFERER"] only works if you enter the page using a hyperlink.
e.g.
/goto.html
go to refer
/refer.php
<?php
print "You entered using a link on ".$_SERVER["HTTP_REFERER"];
?>
HTTP_REFERER doesn't work if you enter the link location directly in your browser.
getenv() is used if it's being run as a CGI script. With a SAPI you use $_SERVER["HTTP_REFERER"].
<?php
echo $_SERVER['HTTP_REFERER'];
?>
The above code works! However, many of my students find it hard, at first, to grasp that $_SERVER['HTTP_REFERER'] requires arriving from a link.
I give them the below (tested) code (or "web page") to demonstrate. The above code is at the bottom.
show-referer.php
<?php
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
$referer = $_SERVER['HTTP_REFERER'];
} else {
$referer = 'No Link - No Referer - Direct URL Entry';
}
echo $referer;
?>
<p>See the referer in action
from this page!
</p>
<?php
echo $_SERVER['HTTP_REFERER'];
?>
The show-referer.php page links to itself when you click the link, which should cause the browser to generate an HTTP_REFERER.
$ref = $_SERVER['HTTP_REFERER'];
Relevant manual page: http://php.net/manual/en/reserved.variables.server.php
If you compute all these answers, you end up with something looking like :
<?php
if isset($_SERVER['HTTP_REFERER']) {
$ref = $_SERVER['HTTP_REFERER'];
}
else {
$ref = "Direct Entry";
}
?>
Again, read http://php.net/manual/en/reserved.variables.server.php:
With HTTP_REFERER there is a comment:
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.