How to create iframe with a dynamic url using user's data - php

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!

Related

Check if URL has ID in Wordpress PHP

I have a page that will show multiple content, depending on what ID the user has in their URL.
e.g:-
If they have accessed https://stackoverflow.com/#cars - I want to show cars only.
If they have accessed https://stackoverflow.com/#trains - I want to show trains only.
I would like to do this in PHP, as I do not want the other information displayed.
I have tried the following, but with no luck:-
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'cars') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
?>
Can anyone please help?
The # part of a URL is an instruction to the browser to navigate to that element of the page as soon as it loads. It's never sent to the server.
If you want to pass information to the server then use query parameters e.g. https://www.example.com?section=trains and then you can use $_GET["section"] in PHP to retrieve the submitted value.
But as someone has noted in the comments, that's a generic way to do this in a web/PHP context, however Wordpress probably has better methods built in to accomplish the specific filtering task you're interested in here.

Permalinks with PHP

I am currently working on a website I am making from SCRATCH :) Anyhow users will be able to create accounts (login and register pages already done), and then they will be able to view there profile. Now the problem I am running into is each user needs there own permalink for there profile's URL. I do not know how to go about doing this though! :( All I can find is using Wordpress to do it for me, but I do not want that. I would like to go about doing this with only PHP and HTML/CSS. Please help me out, thank you! :)
UPDATE 1
Thank you for the relpys, I am still a little confused though. Now if I use a GET, will that actually save the file? How exactly do permalinks work (sorry very confused! :O )? Like when I create the permalink is that creating a new file as well with the code inside of it? If so what would I be writing for that?
UPDATE 2
Sorry I am specifying what I am really asking. When a user created a account, I want it to AUTO create them a permalink (which would be a new file right?), how would I go about doing that?
UPDATE 3
Ok, so I am basically thinking of the same thing. So when a user creates a account this is the code that is executed:
//Create Account Page
$inputquery = "INSERT INTO users username VALUES :username";
$datasend = $connection->prepare($inputquery);
$datasend->execute(array(':username'=>utf8_decode($username),)); //Inputs it into the database
$username = $_POST["username"]; //The username input, then lets say that the account was created with that
$userprofile = fopen($username . ".php", "w"); //Makes a file with the users info, a PHP function
$text = include ('base.php'); //Grabbing the base layout
fwrite($userprofile, $text); //Setting the base layout to the userprofile page
So once that is one the code will be in the file. Then when the page is loaded, that so called "base" code is still there, and then is replaced with the details of the user. That is my rough attempt at this, it should work! :) Thank You for all of your help! :D
You can use a GET request, which will be stored in the URL. You just access it with $_GET['user'], and the url will be in the form my.site.on.the.net/user.php?user=3141. This URL can be stored in bookmarks, put in a webpage, or whatever else and it will link to that user's profile.
This will not save the file by itself. You will need to store the user information somewhere outside your web root, then use the PHP file to access that info based on the ID. You don't want to store the entire webpage; just store the information that is unique to that user (On SO, that would be stuff like their About Me, reputation, badges, etc.)
EDIT: Simple example (note that it doesn't have any password protection or anything else to prevent any random stranger from accessing it)
user.php:
<?php
function getPoints(){
// get the number of points from your data source
}
function getName(){
// get the name from your data source
}
?>
<!DOCTYPE html>
<html>
<body>
<img src="logo.png" alt="logo" />
<h1>Hello, <?php echo getName();?>!</h1>
<p>You have <?php echo getPoints();?> points.</p>
</body>
</html>
In this example, the data source only needs to store the user's name and number of points. The single PHP file fills in the rest of the page. To create a new user account, you will need to add the data for the user to your data source.

PHP - Use GET to include cms (above web root), how to include other files from the cms index?

I am trying to hide my websites cms application...
So i thought i would add a bit of php to any random page on my site, that includes a GET referance to some random string... So basically, if you go to x page, and add ?RANDOMSTRING the cms index is included. This is stored above the web root... Here is the peice of php:
if (isset($_GET['J7sd-H3sc9-As3R']))
{
require_once($docRoot . '/../../includes/admin/index.php');
}
Basically, index.php is laid out as a page with 3 fieldsets. In the 3 field sets are various links relating to various applications that deal with various tasks. They were accessed through the same means as the above code. And they were held in the web root and were able to be accessed via http...
That all worked perfectly fine, But the problem now comes when i try to access any specific part of the cms...so what would have been:
http://www.mysite.com/admin/part/
is now:
include($_SERVER['DOCUMENT_ROOT'] . '/../../includes/admin/part/index.php');
Or something of the sort...
So now when i go to my page at
http://www.mysite.com/randomDirectory/
and add:
http://www.mysite.com/randomDirectory/?J7sd-H3sc9-As3R
I get sent to my cms... Cool... But when i try to click on any section i get this header:
http://www.mysite.com/randomDirectory/?part
and the page gets refreshed to:
http://www.mysite.com/randomDirectory/
If that makes sense...
Could any provide me with any input or suggestions regarding the task that i am trying to accomplish? I am not sure if it is even possible to start off with, but it seems simple enough.
Any replies would be greatly appreciated, Thanks!
I guess you should append at the end of every link in your page something like
<?php if (isset($_GET['J7sd-H3sc9-As3R'])) echo '?J7sd-H3sc9-As3R'; ?>
Example:
http://www.mysite.com/randomDirectory/randomPage<?php if (isset($_GET['J7sd-H3sc9-As3R'])) echo '?J7sd-H3sc9-As3R'; ?>
edit
An easier way to do this would be to use sessions, in this way:
<?php
session_start();
if (isset($_GET['J7sd-H3sc9-As3R']))
{
$_SESSION['token'] = 'J7sd-H3sc9-As3R';
}
if (!isset($_SESSION['token']) || $_SESSION['token'] !== 'J7sd-H3sc9-As3R')
{
exit;
}
// go on with your page
?>
In this way, when you open a page with your token in the url, the session is started and the token is saved in the session, so it should work without the need to insert the token in every url until you close your browser.

Prevent loading PHP pages into Joomla wrapper

I'm owner of some web site with dozen of web pages. Pages were made by using PHP. Before some time I discovered that some guys by using Joomla CMS and wrapper menu option included starting (login page) there and on this way confused members and other visitors, especially because "window" of wrapper isn't enough big and some information on my page aren't visible. On this way visitors connect these pages with me and get bad feeling about whole my site. I contacted these guys but no answer, then I tried to solve it by using $_SERVER['HTTP_REFERER'] super variable but I didn't get right and working solution for this problem. Someone experienced similar problem? Thanks.
EDIT - This is the code
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
if ($HTTP_REFERRER) {
// check if the referrer is on your noentry list
// if so redirect it to another page
if ($HTTP_REFERRER == "www.mean.visitor.com") {
echo 'referer is' . $HTTP_REFERRER;
die;
} // shows the referrer and formats ur local harddrive echo "You came from $HTTP_REFERRER";
} else {
//everything is OK
}
from the code you posted the first problem i see it's on the first line:
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
should be
$HTTP_REFERRER=$_SERVER['HTTP_REFERER'];
Then in the second if you must insert the web addresses you want to block. so change
if ($HTTP_REFERRER == "www.mean.visitor.com")
with
if ($HTTP_REFERRER == "the address yo want to block")
And write die() instead of die.
has something changed?

Change Page From PHP Script?

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>";
}

Categories