So i'm writing a script in PHP which generates a image so other people can use it too.
But is it possible to get the url's on the pages the scripts are used ?
For example.
http://www.johnexample.com is using my image with this format
<img src="http://www.myurl.com/image.php">
Now i wan't to receive the url of http://www.johnexample.com without GET variables if possible.
It's basically a script that's suppose to track/note down all the websites that are using my image.
At first i though it was possible with this:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But that only get's the location of the script itself.
Thanks
Oh, that was simpler than i though.
Got it working like this now.
The page with the tag only has to load once and it will save.
Only using Session now because it's being tested local.
Gonna switch it over to a database.
Thanks guys
<?php
session_start();
$url = $_SERVER["HTTP_REFERER"];
if(!strpos($_SESSION["url"], $url)) {
if($url != '') {
$_SESSION["url"] = $_SESSION["url"] . "," . $url;
}
}
$tracker = explode(",", $_SESSION["url"]);
var_dump($tracker);
?>
Related
This is my domain http://cdtr.cf it lands on index.php
Where I wrote some code which pulls the URL from URL box and trim its suffix to identify visitor's identity.
The Problem is when someone opening http://cdtr.cf/.... it shows error 404 page not found
while just http://cdtr.cd/ is working fine. All I need is When someone visit with http://cdtr.cf/.... they must be redirected to index.php and their request should be processed like http://cdtr.cf/index.php/.....
I want to keep the suffix anyhow for some purpose :-http://cdtr.cf/suffix.
It will be great if all this happen without any visible change in URL box.
Thanks in advance.
$link= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=== 'on'? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $link; //It is working fine on localhost but not when i put it live
This code will give you the rest of the url as per your requirement
<?php
$link = $_SERVER['REQUEST_URI'];
echo $link = ltrim($link, '/');
//if you want you can trim last / too using $link = rtrim($link, '/');
?>
If you want to get middle stuff like this yoursite.com/stuffs/removedthis
then you have to use below code
<?php
$link = $_SERVER['HTTP_HOST'];
$link .= $_SERVER['REQUEST_URI'];
$link = explode('/', $link);
echo $a = $link[1];
?>
Second example:
$link = "example.com/stuff/removethis/....blah";
$link = explode('/', $link);
echo $a = $link[1];
This is the scenario:
I am using Wordpress
I use a plugin that pulls products from a online seller and displays it on my website.
My website is on SSL and the source website has both SSL & non-SSL
The image sources can be both:
http ://somewebsite.com/folder/someimage.jpg
OR
https ://securewebsite.com/folder/someimage.jpg
I want to use a function in my Wordpress theme (or in the plugin itself) that will change part of the image URL before the webpage is displayed.
change the first part of the image URL :
***http ://somewebsite.com/folder/***someimage.jpg
and replace it with
***https ://securewebsite.com/folder/***someimage.jpg
I am absolute noobie with php and wordpress and am learning from all the Guru's here. Would be great if the the answer can be detailed please.
The problem is to solve 'Mixed content' warning from Google chrome.
I did see this piece of code somewhere (on some forum) so I guess something like this should work for my site as well.
function rewrite_image_for_https(&$image_url) {
// Determine if site is being accessed by HTTPS
$secure = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? TRUE : FALSE;
if ($secure) {
// Strip off protocol, and change protocol and domain to point at HTTPS image
$url = str_replace('http://insecureimagecource', '', $image_url);
$url = explode('/', $url);
$url[0] = 'https://secureimagesource';
$url = implode('/', $url);
$image_url = $url;
}
}
I have also tried the below, nothing works :-( Please help
<?php
function rewrite_image_for_https () {
$url = preg_replace("/^http://insecureimages", "https://secureimages", $url);
}
?>
Tried this too..
<?php
str_replace('http://insecureimages.com', 'https://secureimages.com', $url);
?>
There are multiple methods to solve this for example:
str_replace
str_replace('http://somewebsite.com', 'https://securewebsite.com', $url);
parse_url
$parts = parse_url($url);
$secure = 'https://securewebsite.com'.$parts['path'];
I'm currently working to make my own CRM website application and I followed Alex youtube tutorial which is the login/register using OOP.
In addition I need my index.php to be the dynamic content switcher, which I only include header and footer while the content load from a folder where it stores all the page. I believe the end result should be like www.example.com/index.php?page=profile
I look around and it seems like what I'm doing it's something similar to MVC pattern where index is the root file and all the content is loaded from view folder.
I managed to get everything done correctly but now instead of displaying the link like: www.example.com/user.php?name=jennifer
I wanted it to be www.example.com/user/name/jennifer
I try to look around phpacademy forum but the forum seems to be abandon, some search I managed to find a topic that relevant to what I want, but the code doesn't seems to be working and I got the same error with poster.
here is the code:
<?php
// Define the root of the site (this page should be in the root)
define('ROOT', rtrim(__DIR__, '/') . '/');
define('PAGES', ROOT . 'pages/');
// Define "safe" files that can be loaded
$safeFiles = ["login", "regiser", "profile", "changepassword"];
// Get URL
if(isset($_GET['page']) && !empty($_GET['page'])) {
$url = $_GET['page'];
} else {
$url = '/';
}
// Remove Path Traversal
$sanatize = array(
// Basic
'..', "..\\", '../', "\\",
// Percent encoding
'%2e%2e%2f', '%2e%2e/', '..%2f', '%2e%2e%5c', '%2e%2e', '..%5c', '%252e%252e%255c', '..%255c',
// UTF-8 encoding
'%c1%1c', '%c0%af', '..%c1%9c'
);
$url = str_replace($sanatize, '', $url);
// Prevent Null byte (%00)
// PHP 5.6 + should take care of this automatically, but PHP 5.0 < ....
$url = str_replace(chr(0), '', $url);
// Filter URL
$url = filter_var($url, FILTER_SANITIZE_URL);
// Remove any extra slashes
$url = rtrim($url, '/');
// Make lowercase url
$url = strtolower($url);
// Check current page
$path = PAGES . $url . '.php';
// If the file is in our safe array & exists, load it!
if(in_array($url, $safeFiles) && file_exists($path)) {
include($path);
} else {
echo "404: Page not found!";
}
I search around Google but I couldn't find a solution and I notice there were people asking in this forum as well hence I hope someone can assist me in this area.
i am trying to send email from php
i have one php file with all values & other php template file.
(both files are on same server)
i am using file_get_contents to get contents of php template file for example
$url="emil_form.php";
$a="uname";
if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
$Content = "";
}
...... EMAIL Sending Code ..........
and here is code for emil_form.php (email template file)
Your Name is : <?php $_GET['uname']; ?>
so once i got data in $Content i can send it by email.
but i am getting error unable to open file....
what i want is pass data from original php file to template php file and what will be output of template stored in variable so i can send it by email.
how can do this ?
Thanks
The real problem would be that you try to read a local file with a http query string behind it. The file system don't understand this and looks for a file called "emil_form.php?uname=uname".
$_GET / $_POST / etc only works over a http connection.
Try to put a placeholder like "%%NAME%%" in your template and replace this after reading the template.
<?php
$url = "emil_form.php";
$a = "uname";
if(($Content = file_get_contents($url)) === false) {
$Content = "";
}
$Content = str_replace('%%NAME%%', $a, $Content);
// sending mail....
Template will look like this:
Your Name is: %%NAME%%
Here's an alternative solution...
Use the relative path or absolute path as suggested by "cballou" to read file.
But insted of wirting a php file use a simple text file put you message template in it replace <?php $_GET['uname']; ?> with something unique like %uname% (replacement keys).
Read the file content into a variable and replace the replacement keys with your variable like so,
$url = "/usr/local/path/to/email_form.txt";
$a = "uname";
if(($Content = file_get_contents($url)) === false) {
$Content = "";
}else{
$content = str_replace('%uname%', $a, $content);
}
$url = sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
$content = file_get_contents($url."/file.php?test=".$test);
echo $content;
Please note that you had a filename error in $url which wss trying to load emil_form.php.
A secondary issue seems to be the path you are using for your call to $url. Try making it an full URL in order to properly parse the file, avoiding any templating you may otherwise have to do. The RTT would be slower but you wouldnt have to change any code. This would look like:
$url = 'http://www.mysite.com/email_form.php';
$a = "uname";
if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
$Content = "";
}
I have a comment system that allows auto linking of url. I am using cakephp but the solution is more just PHP. here is what is happening.
if the user enters fully qualified url with http:// or https:// everything is fine.
but if they enter www.scoobydoobydoo.com it turns into http://cool-domain.com/www.scoobydoobydoo.com. basically cakephp understands that http|https is an external url so it works with http|https not otherwise.
My idea was to do some kind of str stuff on the url and get it to insert the http if not present. unfortunately whatever i try only makes it worse. I am noob :) any help / pointer is appreciated.
thanks
EDIT: posting solution snippet. may not be the best but thanks to answer at least I have something.
<?php
$proto_scheme = parse_url($webAddress,PHP_URL_SCHEME);
if((!stristr($proto_scheme,'http')) || (!stristr($proto_scheme,'http'))){
$webAddress = 'http://'.$webAddress;
}
?>
$url = "blahblah.com";
// to clarify, this shouldn't be === false, but rather !== 0
if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
$url = "http://{$url}";
}
Try the parse_url function: http://php.net/manual/en/function.parse-url.php
I think this will help you.
I've had a similar issue, so I created the following php function:
function format_url($url)
{
if(!$url) return null;
$parsed_url = parse_url($url);
$schema = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
return "$schema$host$path";
}
if you format the following: format_url('abcde.com'), the result will be http://abcde.com.
Here is the regex: https://stackoverflow.com/a/2762083/4374834
p.s. #Vangel, Michael McTiernan's answer is correct, so please, learn your PHP before you say, that something might fail :)