Current Page Name - php

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

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!

PHP GET variable with dash

The URL is something.php?id=123-10-1
If I echo it, it only prints out 123 but I need it to say 123-10-1.
I suppose the solution is very simple but I'm not seeing it.
Use php urlencode and urldecode functions.
Of course, You will have the full argument stored in GET array. You are not doing anything wrong. The mistake should be somewhere else. Try to use:
var_dump($_GET);
$equal = ('123-10-1' == $_GET['id']);
var_dump($equal);
To see what is wrong... Echo is not the best printing functino here, however, it also should print the full argument...
UPDATE - note
After we know what was wrong:
$id = isset($_GET['id']) ? (int)$_GET['id'] : '';
I would suggest something like that:
// Set part of the code
$id = isset($_GET['id']) ? $_GET['id'] : '';
// Verification part of the code
if (!is_numeric($id) && $id != '') {
throw new Exception('ID must be numeric.');
}
if ($id == '') {
// ID was not set in the url. Maybe there should be another action here?
}
IN that case, You are in full control of what is happening here. Modyfing GET values or POST values "on the fly" is not the good practice.
I hope it helps.
Try using an anchor tag like <a href="something.php?id=123-10-1"> and it will echo 123-10-1
You are searching for the function urlencode which encoding a string to be used in a query part of a URL.
This was my code.
$id = $_GET['id']
echo $id; //123
and it printed '123' only.
But
echo $_GET['id']; //123-10-1
printed it all. Seems weird stuff as it should work in the first place, but it works now.
Gotta check my php.ini to see what's this all about.
I have try your code it giving me correct response
-<?php echo $_GET['id']; ?>
Output 123-10-1

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"); {

Passing unknown link variables to a new page

I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1

Wordpress: Saving the Page URL as a variable

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

Categories