How To Pass php Variable To another php file [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have created a custom router. For Example
$istek = $_SERVER['REQUEST_URI'];
switch ($istek) {
case '/' :
$title = "Anasayfa";
require base . 'views/index.php';
break;
So I want to pass the example id variable with $_GET. But the router blocks it and redirects to the 404 page. how can I pass this variable without getting any errors or redirecting to 404?

The reason is that $_SERVER['REQUEST_URI'] includes everything, not just the / so your switch will fail.
To fix, use parse_url, / is the PHP_URL_PATH, whilst ?id= would be in PHP_URL_QUERY.
So you could use something like this to get what you want from $_SERVER['REQUEST_URI']:
<?php
// example
$_SERVER['REQUEST_URI'] = '/controller/action?id=123';
$uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri);
$route = explode('/', ltrim($uri['path'], '/'));
print_r($route);
parse_str($uri['query'], $query);
print_r($query);
Result:
Array
(
[0] => controller
[1] => action
)
Array
(
[id] => 123
)
https://3v4l.org/UMFQF

Related

How to allow access for one specific IP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
I need to allow access to one specific WordPress page for one IP. It works fine in normal browser, but in anonymous browser with VPN i can still open the page.
This is my code:
function restrict_access_by_ip_address() {
$allowed_ips = array( '11.11.11.111' ); // replace with your own IP addresses
$current_ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
if ( ! in_array( $current_ip, $allowed_ips ) && is_page( '2533' ) ) {
wp_die( 'Access Denied' );
}
}```

Check, if is allowed embedding ( youtube video ) PHP, new youtube API [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
can someone hep with replace old way to new YouTube API?
I am looking for check option - allowed embedding.
My old code in PHP:
$url="http://gdata.youtube.com/feeds/api/videos/" . $videoID . "?v=2&alt=jsonc&prettyprint=true";
$json = file_get_contents($url, true);
$json_output = json_decode($json);
echo "emb = ".$json_output->data->accessControl->embed;
I am looking for easy, fast code, because i need to check more videos at time.
Thx.
The new base URL to get information about a video, through the ID, like http://www.youtube.com/watch?v=abcdefghijkl is:
https://www.googleapis.com/youtube/v3/videos?id=abcdefghijkl&key=YOUR_API_KEY&part={parts you want}
To get the status, if it's embaddable you have to set part to status, so your request looks like this
https://www.googleapis.com/youtube/v3/videos?id=abcdefghijkl&key=YOUR_API_KEY&part=status
The response output will look something like this:
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true
}
Source: YouTube API - Getting started and YouTube API - Reference Videos
I didn't test it, but I hope it will work and I could help you!

Apache Fallback instead of add_rewrite_rule [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
My requirement is to do SEO Friendly URL for an existing site in Wordpress.
For example,
Current URL: http://localhost/test/?pid=19971
that is the page "test" is rendering the data from some custom plugin.
My new SEO URL : http://localhost/brand/sub_category/product_name
Here, Brand, Sub_category and product_name is fetched from the pid..
I am populating the data using the following rewrite rule from functions.php,
add_rewrite_rule(MY_DYNAMICALLY_POPULATED_URL.'$ page-test.php?brand='.$brand_name.'&sub='.$sub_category.'&pname='.$product_name.' [L]', 'top');
In this code, the page-test.php is placed in the root directory and the data is rendered from that file by passing the brand_name, sub_category and productname to fetch the pid. So based on the pid each product page is rendered.
This code is working fine for me, When I save the data automatically the data is written in .htaccess and page is rendering with the new SEO URL.
But when my client needs it to do it by FALLBACK in Apache instead of add_rewrite_rule due to some load balancing issue.
So can anyone help me how to do the same with Fallback resource.
This fixed my issue.
function my_query_vars($vars)
{
$my_vars = array( 'PARAMETERS' );
return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');
function my_rewrite_rules($rules)
{
global $wp_rewrite;
$my_page = 'PAGESLUG';
$my_rule = array( 'URL/?' => 'index.php?pagename=' . $my_page . '&school_type=$matches[1]' );
return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');

Translate website in many language [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 site have an folder language where have:
en.php
<?
$lang = array(
'sp1' => 'Welcome',
'sp2' => 'Home',
);
?>
and it.php
<?
$lang = array(
'sp1' => 'Benvenuto',
'sp2' => 'A casa',
);
?>
In the index.php i have like:
<h4><?=$lang['sp1']?></h4>
<a><u><strong><?=$lang['sp2']?></a></u></strong><br />
But this is an option for change the language from cpanel, how I can transform to the geo language.. when an italian visit my site can view my site in italian language, etc?
You can use $_SERVER['HTTP_ACCEPT_LANGUAGE']
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
//echo "PAGE FR";
include("fr.php");
break;
case "it":
//echo "PAGE IT";
include("it.php");
break;
case "en":
//echo "PAGE EN";
include("en.php");
break;
default:
//echo "PAGE EN - Setting Default";
include("en.php");//include EN in all other cases of different lang detection
break;
}
?>
Browsers advertise the user's "preferred" language in an HTTP header called Accept-Language, which you can read in PHP as $_SERVER['HTTP_ACCEPT_LANGUAGE'].
There is actually a fairly complex structure to this header, allowing multiple languages to be given priorities, and the language itself can take a few different forms. The answers to this previous question discuss how to parse it fully.
But as a first approximation, you can just take the first two letters, which will generally be one of the ISO 639-1 codes listed here by the US Library of Congress or here on Wikipedia:
$default_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
However, this should never be the only way of selecting the language. As explained fairly clearly by this W3C document, the browser might not be correctly configured, or the user might not be using their own computer, so that it would not be sending the correct value.
Instead, you should treat this as a default, and allow the user to over-ride it. This can be as simple as providing links on every page which add ?lang=it (etc) to the current URL. Then set a cookie containing their preference, and ignore the accept-language from then on:
if ( $_GET['lang'] ) {
// User asked for a particular language; obey, and remember in a cookie
setcookie('lang', $_GET['lang'], 0, '/');
$preferred_language = $_GET['lang'];
} elseif ( $_COOKIE['lang'] ) {
// Cookie found from previous selection
$preferred_language = $_COOKIE['lang'];
} else {
// Guess based on browser settings
$preferred_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}

abraham/twitteroauth reply to a perticular tweet in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hi i am totally new here and title says it all ..
i want to reply to a tweet using abraham/twitteroauth library for twitter .. update for the twitte is working fine but i am unable to do reply thing .
I am doing this ..
$status = $connection->post('statuses/update', array("status" =>
$dataa, "in_reply_to_status_id" =>$id));
twitteroauth_row('statuses/update', $status, $connection->http_code,
$parameters);
this code also updates the account with value in $dataa variable but do not reply to the tweet defined in $id
Please help .
Sorry for my bad english .This is my first post.
Thanks
yap . there was mistake from my side while updating the post .. after reading api document .. i found that without #username sign to perticular user it won't appear in his reply section ..
hamdoulilah the solution is so easy
you need just to add #username for who you want to replay to
$status_id = '480775609728385024';
$twitt_reply = '#username the replay text';
$responce = $connection->post('statuses/update', array('in_reply_to_status_id'=> $status_id , 'status' => $twitt_reply ));

Categories