Wordpress: Saving the Page URL as a variable - php

I have a function in Wordpress that I BELIEVE should be getting the URL of the page a visitor is sitting on. It's as follows:
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
Then, for fun, I have a PHP enabled widget with:
<?php echo get_request_url() ; ?>
However, it always shows no value. I don't get errors, just... nothing.
I'm a pretty novice coder, and I hacked this together from a few different sources (I know, I know... search engine code snippet alchemy can be bad for your health), so I think I've either skipped a step, or missed the point entirely.
Can anyone point me in the right direction?

Your function needs to add a return
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return $formurl;
}

Try:
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return $formurl;
}

Related

check the value returned by array_rand?

I'm trying to get some PHP to check if a specific value is pulled out of an array, but I'm having trouble getting it to work. It actually causes the site to go black.
<?php
$message_array = file("http://www.example.com/wp-content/themes/mytheme/Subtitles.css");
$message = array_rand($message_array);
echo "$message_array[$message]";
$GfCheck = "<audio id='audio' src='example.com/wp-content/uploads/2016/01/example.wav'; preload='auto' ></audio><a onclick='GFFUNC()'><img src='example.com/wp-content/uploads/2016/01/Gf.png'; height='90px' alt='gf' title='gf'/></a>";
if ($message_array[$message] == $GfCheck) { $Gf = "1" } else { }
?>
the $Gf would then in turn add a secret section to a menu later on.
Can anyone help me figure out what's going wrong?
Thanks in advance for your help!
Answer largely came from Rizier123's comments on the main post.
While the file tag did not work, the code did in the end. I am left with an issue of global variables now, but the hard part is done!

Current Page Name

I am new in php. I want to show the current page name as my page title. So I used...
<?php echo ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)); ?>
Now I want to show the page title as "Home" if the "ucfirst" code returns "index" i.e. my index.php. So I tried to write a function.
function pagename() {
$pname = ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME));
if($pname == "Index") {
$pname = "Home";
}
return $pname;
}
But the problem is, it is not showing anything. I know my code is wrong, but I failed to understand where is wrong. Please help.
I tried your code, it's giving me correct output. when i run it, I get "Index" as return value. maybe you should see that you are printing $pname correctly or not in the application :) Good luck

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

Running a function by typing in URL

I have searched all over the internet to run a function from URL and found out it is not possible.
I am asking if there is another way to do this. I will explain below what I am trying to achieve.
On my website I have several functions that when a hyperlink is clicked on that cracks.php page, the main content changes accordingly. Now I want to direct people from a single URL to that cracks.php page with the function already called.
Example - www.example.com/cracks.php?AC ( which will call a function named AC and the content changes before the page loads)
Ive found this method below, but couldnt get it to work.
if(document.location.search == '?AC')
{
AC();
}
Sorry for the messy code on the website. Thanks for reading, any help would be appreciated.
You can call www.example.com/cracks.php?do=AC and then get do with $doMethod = $_GET['do'];. What you then do is, use a switch function or a few ifs to check and execute when e.g. $doMethod equals AC.
Like this:
$doMethod = $_GET['do'];
switch($doMethod)
{
case "AC":
//some random stuff to do
break;
case "BD":
//some random stuff to do
break;
case "XY":
//some random stuff to do
break;
default:
break;
}
That depends if you need to do that dynamically or you can do it hard coded. Because that hard coded is too simple (with if's and switches), what you have to do is:
$functionsList = Array('func1', 'func2');
function func1(){
echo '1';
}
function func2(){
echo '2';
}
if (function_exists($_GET['f']) and in_array($_GET['f'], $functionsList)){
call_user_func($_GET['f']);
}
Then call your_file_name.php?f=func1 and your_file_name.php?f=func2 and you'll see different outputs.
With the help of Mark Koopman I managed to use his Javascript method and it worked like I wanted.
So heres the method in Javascript:
<html>
<body>
<script>
function handleOnload()
{
if(location.search == "?AC")
alert("the query string is " + location.search);
}
window.onload=handleOnload;
</script>
</body>
</html>

php , read file code problem

I was using this piece of php code for a site.
Now its old and I recently had a few attacks. Script was used for to include another file from someplace else and send spam. Obviously this makes my script as spam sender.
for the content
$htm = ".htm";
$pid = "$details$htm";
function show_details($pid)
{
if (!preg_match("/http/", $pid)) {
require($pid);
} else {
die;
}
}
and for the title, desc , keywords etc..
$txt = ".txt";
$title = "$details$txt";
function show_title($title)
{
if (!preg_match("/http/", $title)) {
if (file_exists($title)) {
require($title);
} else {
die;
}
}
}
and a display.php file with
print '
<!-- CONTENT -->
';
show_details("$pid");
print '
by this code ı was able to call any content by "/display.php?details=mycontentpage"
mycontentpage.htm
mycontentpage.txt
.............
Now this code has to be re-coded .. I can not change the construction as the site is just too big.
So I guess I just have to stick to this..
Can anyone help ? Any comments ?
To make scripts like this more secure, you have to ensure register_globals is set to OFF. This means you'll have to add a line like:
php_flag register_globals off
...To .htaccess. Then, declare all your user variables the first time you use them like:
$details = $_GET['details']
...Which assigns the data from the URI piece "details" to the PHP variable $details.
I can very much see how your attackers were able to get in via your code and register_globals set to on -- they'd need to merely create a .htm file with PHP code in it that reassigns other variables, include it, then viola.
For more info, see:
http://us2.php.net/manual/en/security.globals.php
Hope this helps!

Categories