PHP: Assign $page variable if page url is = to '?' [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a way in PHP to assign or display a variable if the page URL is equal to a specific value?
I have a site with 100 pages. Each page has a list of variables that are echoed through it based on $page variable. I would like to have this variable assigned on the fly. I want to be able to set a variable, 1-100 for example, based on page URL.
If page URL is equal to
www.example.com/page/dog
set the $page variable for page to dog.
If page URL is equal to
www.example.com/page/cat
set the $page variable for page to cat.
So on and so forth. Once the variable is set certain things will happen. In order for the correct information to be displayed I need the correct page variable to be set.
Below is ALL the code that exists on each page.
<?php $page = 'example1'; ?>
<?php include '../includes/core/overall/header.php'; ?>
<?php include '../includes/core/overall/footer.php'; ?>

Use the following:
$page = basename($_SERVER['SCRIPT_FILENAME']);

You can combine basename($_SERVER['SCRIPT_FILENAME']) like this:
<?php
include '../includes/core/overall/header.php';
include '../includes/core/overall/footer.php';
$var = basename($_SERVER['SCRIPT_FILENAME']);
//get page with .php
$var = preg_replace('/\.php$/', '', $var);
//remove .php from end
if($var == "dog"){
$page = "dog";
}
else if($var == "cat"){
$page = "cat";
}
else{
$page = "foo";
}
echo $page;
?>
Hope this helps!

Take advantage of HTTP's pathinfo.
$DOCUMENT_ROOT/page/index.php:
<?
$page = substr($_SERVER['PATH_INFO'], 1);
...

Related

Call a php function passing argument on a link in the same page and display the content in div [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to call a function by passing argument on a HTML link in the same page
My code:
<?php
#array loop
foreach($new_array as $value){
#pass the array to the function
$input = inputFiles($value);
# call the function on the click of link
echo 'Input Link';
# output of the function in the div
echo '<div id="inputLinkOutput" ></div>';
}
function inputFiles($value){
# get the value and do the rest work
}
?>
Please help.
So you pass $_GET variable via url like that:
<!-- lcoalhost/myPage.php -->
<a href='myPage.php?var1=123&var2=abc+bcd'>Link here</a>
And in PHP:
$var1 = $_GET['var1']; // => (string) '123'
$var2 = $_GET['var2']; // => (string) 'abc bcd'
You need to use something like Ajax to achieve this. Here how you do this.
Create a ajax function which calls a method in a php file. Along with the call pass a parameter with a value. Because this will help in the server side to execute which function.
Then inside the PHP file create set of functions which you think will execute. (PHP function has been listed below)
Introduce a switch statement to navigate to specific function.
$param = $_GET['val'];
switch($param){
case '1' : methodA(); break;
case '2' : methodB(); break;
//and so on
}
methodA(){
//do some logic
}
methodB(){
//do some logic
}
Hope this helps.
Cheers!

Php variable not showing up? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to change the picture for facebook thumbnail by having php set the image in the meta tag depending on the page. I have this code but for some reason when I go to Facebook and debug it it actually shows the variable and not what the value of the variable is. Here is my code please help thank you!
<?php
if($ifpage == 'picture.php')
{
$metaimage = '<meta property="og:image" content="http://www.quickdailylaugh.com/photos/print $picture" />';
print($metaimage);
}
?>
Your variable is inside of a string enclosed with single quotes. Single quotes will take the literal value of the string $varname and not translate the variable to it's value. You need to use double quotes. Example:
$var1 = 'test';
$foo = 'The value of var1 is: $var1'; // The value of var1 is: $var1
$bar = "The value of var1 is: $var1"; // The value of var1 is: test
To interpret the varible you must use double quotes.
<?php
if($ifpage == 'picture.php')
{
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/print $picture\" />";
print($metaimage);
}
?>
I had to do another query for it to show... Thank you all for the answers here is what i used...
<?php
if($ifpage == 'picture.php')
{
$photoid = $_GET['picture'];
$sql = "SELECT * FROM photos WHERE id=$photoid";
$pic = mysql_query($sql);
while($row = mysql_fetch_array($pic))
{
$picture=$row['picture'];
}
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/$picture\" />";
print($metaimage);
}else{
?>

Creating custom ahref links in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you create a custom links in ahref using php as shown below
link from : http://www.top10bestwebsitehosting.com/visit.php?site=iPage
goes to : http://www.ipage.com
Perhaps something like this:
$sites = array(
'iPage' => 'http://www.ipage.com/',
'Google' => 'http://www.google.com/',
);
$key = $_GET['site'];
if(isset($sites[$key])) {
header('Location: ' . $sites[$key]);
exit;
}
echo 'Sorry, no such site.';
There is redirect, if you open in firefox developer tools and after open a link, and pressing escape key(else you will be redirected) you can see the next url:
http://naturaltracking.com/redirect.php?url=http%3A%2F%2Fwww.ipage.com%2Fjoin%2Findex.bml%3FAffID%3D638751%26amp%3BLinkName%3D&uid=6344b6a239bb&sid=374eaa4bb6d2&hukc=1&nicid=00004ZQNza&nivkey=FiiRIhiDHa
It was most likely done in this way:
have an internal list of what $_GET['site'] can be
read $_GET['site']
redirect to the corresponding website
This is an example:
<?php
// visit.php
$pages = array('iPage'=>'www.ipage.com','google','www.google.com');
if (in_array($pages,$_GET['site']))
{
// do some internal logging or whatever
header("Location: ".$pages[$_GET['site']]);
}
?>
The way top10bestwebsitehosting does is, it keeps the urls in the database. So then it does something similar to the below:
$name = mysql_real_escape_string($_GET['site']); //check for escapes etc.
$query = mysql_query('select * from `websites` where `name` = {$name} limit 1');
$row = mysql_fetch_row($query);
header('Location: '.$row['url']);

how to rout the request when every page is going from index page in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
my project every page is going through index page through htaccess...so how to redirect each page..here is my index.php page which include first page..but when i request another page..it not redirect to that page...only blank page is displayed..
require ('class/class.database.php');
require ('config.php');
$requestURI = $_SERVER['REQUEST_URI'];
$requestURI = str_replace(SUBDIR,'',$requestURI);
$requestParse = explode('/',$requestURI);
$page = array_shift($requestParse);
print_r($requestParse);
if($page == "")
{
$page = DEFAULT_PAGE;
}
include 'views/'.$page.'.php';
Suggestions for debugging:
echo $page just before your include to ensure it is what you expect it to be. Your code echoes a previous variable which is not the one you end up using.
create a views/$page.php file that does something simple like echo('yay');
Call your simple page and see if it does what you expect.
i hv solved it..now it redirect to page after index page
$requestURI = $_SERVER['REQUEST_URI'];
$requestURI = str_replace(SUBDIR,'',$requestURI);
$temp = explode('?',$requestURI);
$requestURI = array_shift($temp);
$requestParse = explode('/',$requestURI);
$page = array_shift($requestParse);
$action = array_shift($requestParse);
if($action == '')
{
$action = DEFAULT_ACTION_PAGE;
}
$includeFile = 'views/'.$page.'/'.$action.'.php';
if(file_exists($includeFile))
{
require $includeFile;
}

How would I get an alarm using sessions when variable change? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Im trying to build a single page that gets content using "file_get_contents" function, I have managed this part. The content ends up in a variable called $content. The idea is to get an alert (by sound) when the content changes.
What I have faild at is the part where I compare the old value for $content with the new one. Somehow I must keep the old value and compare it with the new one and when I reload the page I would like to get an alert if $content has changed. I was thinking I should use sessions for this comparison.
This is what I got:
<?php
session_start();
$_SESSION['red'] = $red;
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
It is not working, probably looks like crap since I have no clue what im doing really :)
Below is the code for setting the $content variable:
$page = file_get_contents('data.htm');
if ( preg_match ( '/<a tabindex="50" class="item_link".*?a>/s', $page, $matches ) )
{
foreach ( $matches as $key => $content )
{
echo $key . ' => ' . $content . '<br /><br />';
}
}
else
{
echo 'No match';
}
?>
For the Alert sound I found a script;
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
Need help activating this script if the $content has changed, also to make it only play once.
I commend your self-efacing attitude.:)
One problem, I think, is the way you've written your if statement(s):
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
Firstly, the '.' after echo "ALERT" should, I think be a ';'.
Secondly, use an else rather than the 2 if statements. Why write more code than you need to and (more importantly) the assignment in your first if makes your second if true!!
Also, I think you want to be making $red = $_SESSION['red'] at the start, then make $_SESSION['red'] = $content.
Finally, use strcmp() instead of == or != (strcmp() is more reliable for comparing strings - remember it returns false when the strings match though).
So, you end up with:
<?php
session_start();
$red=(array_key_exists('red',$_SESSION)?$_SESSION['red']:'');
... set up $content
if(strcmp("$red","$content") )
{ // what you've previously read doesn't match the new content
echo "ALERT";
// escape back to the html to do the sound stuff
?>
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
<?php
$_SESSION['red'] = $content;
// so now they both match the new content
// - so will give "same" next time round
} else {
echo "Same";
}
...
Try this out.
First, you must consider that to re-run your PHP code, your page needs to refresh.
To achieve this, you may use the classic html refresh through meta tag (I suppose you want just get things done, otherwise you need some tutorials to understand better what you are doing).
Second, you can store the content of data.htm in a file on the server (it's not clever how data.htm is supposed to change, I imagine through FTP or something similar, if it should change through browser, notice that it's a completely different story and you have to follow some tutorials).
After you have done this, you can just compare file_get_contents result with the stored file and check if they are not the same. If they are not, print out your javascript code to have your page ring and replace the content of the file with the new fetched contents (otherwise will keep ringing).
No sessions in this way.

Categories