PHP hand variable to another php document [closed] - php

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
I'm trying to get a variable which I declared in one php file to another without including the whole first php
while($row = mysql_fetch_assoc($sql)) {
// Urlaubstage ausgeben
if($row['frtutage'] < 1) {
$verbraucht = "0";
} else {
$verbraucht = $row['frtutage'];
}
$resturlaub = $row['miturlaubstage'] + $row['mitutagevorjahr'] - $verbraucht;
$urlaubgesamt = $row['miturlaubstage'] + $row['mitutagevorjahr'];
I need the variable $resturlaub in the second PHP without calculating the variable again.
How do I do this? Or is it even possible?
Thanks.
edit: the first php file is about calculating vacation days and how much I have remaind after taking a few vacation days, in the second file I need the calculation of the remaining days then, so I just want to use the variable again and not calculate it again

You can try somehting like
$var = 'random_query';
$page= 'yourpage.com/?my_var='.serialize($var);
header("Location: $page");
exit;
and in your page you can get the value by
if (isset($_GET['my_var']))
{
$my_var = unserialize($_GET['my_var']);
}
But it would depend on the size of that variable that you need to pass, and what is the purpose of the scripts.

If you don't want to include the whole first php file but only a variable then you should create a third file (called: variables.php or config.php for example).
Then include variables.php in both file so the variable will be shared among your scripts

Related

Getting the part of the current URL in Laravel 7/8 [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 1 year ago.
Improve this question
How to get the current URL without the first part dynamically?
For example:
www.google.com/en/second => /second
www.google.com/en/second/third => /second/third
Where to put the function or how to implement this in the current blade view?
You can use Request::segments:
implode('/', array_slice(request()->segments(), 1));
All http link:
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // www.google.com/en/second/third
Just requested link:
$requested_link = "$_SERVER[REQUEST_URI]"; // en/second/third
If you do not want any part of link, replace it with "":
str_replace("en/", "", $requested_link); // second/third
You can put this code anywhere in view or controller. For example in view:
<?php
function get_url(){
$requested_link = "$_SERVER[REQUEST_URI]";
return str_replace("en/", "", $requested_link);
}
?>
Get the current URL including the query string...
echo url()->full();

Allow unique $_GET request only? Generate, verify, forbid values [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 3 years ago.
Improve this question
Hello everyone again here,
I want to create a PHP script for my software which generates and returns the specific code using one $_GET request with a string and using another verificates this code, then forbid running same string.
Something what should work like this:
1st user's software runs "http://example.com/codes.php?create=" and string like "abc".
and script returns code based on "abc", e.g. "4aO45k", "12sdF4" etc.
2nd user's software runs "http://example.com/codes.php?verify=" and this code.
If this code exists, return true and remove it FOREVER, meaning this code will never be generated again. If this code doesn't exist, return false.
If 1st user's software will run "http://example.com/codes.php?create=abc" another code will be generated.
In simple words:
if $_GET is create, then
generate random alphanumeric string, save it and return
if $_GET is verify, then
check if this string exists, if so, then
return true, remove from saved
otherwise
return false
Possible without databases, SQL, mySQL, FireBird...?
How do I make it using .ini files as storage?
Thanks.
It's possible with files. You can do something like the simple solution below:
A couple of notes:
I don't know what you intend by based on exactly, so this just uses the input as a prefix
This stores every code in a file indefinitely; if used a lot this file will grow very large and checking for the existence of codes, and ensuring new codes are unique can grow very slow
The same code can be verified multiple times, but will never be recreated. Marking them as used after verification is of course possible as well
As a general rule don't go creating global functions and shoving everything in one file like this. It's really just proof of concept of what was asked
<?php
$file = fopen('codes', 'a');
if (!empty($_GET['create'])) {
$seed = $_GET['create'];
do {
$code = uniqid($seed);
} while (codeExists($code));
fwrite($file, $code . "\n");
echo $code;
}
else if (!empty($_GET['verify'])) {
echo codeExists($_GET['verify']) ? 'found' : 'not found';
}
function codeExists($verification) {
$file = fopen('codes', 'r');
$found = false;
while ($code = trim(fgets($file))) {
if ($code == $verification) {
$found = true;
break;
}
}
return $found;
}

Compare two URL-s with or without www 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 5 years ago.
Improve this question
I have Iframe that inserts data into my DB from another websites using $_SERVER['HTTP_HOST'] value.
One of the column inserted is website URL that can start with
www (e.g: www.viber.ge) or without it (just viber.ge).
I need to compare URL that Iframe has already inserted into DB to URL that Iframe is on at the moment.
But there can be www as subdomain name
So how can I be sure starting www is subdomain or not?
(I putted hello at the top but it is not showing up -_- )
Question is not about comparing "www.viber.ge" with "viber.ge"
it is more about comparing "www.www.viber.ge"(which I think can be inserted into the DB as "www.viber.ge")
with www.viber.ge (which I think can be inserted into DB as "viber.ge" or "www.viber.ge").
Additional question:
is it possible user to go to "www.www.viber.ge" and $_SERVER to save it as "www.viber.ge" (subdomain)?
You can use str_replace and remove the shorter url from the longer.
If what is left is "www." Only then it's the same domain.
$url1 = "www.viber.ge";
$url2 = "viber.ge";
If(strlen($url1) > strlen($url2)){
If(str_replace($url2,"",$url1) == "www."){
Echo "same domain";
}Else{
Echo "not same";
}
}Else{
If(str_replace($url1,"",$url2) == "www."){
Echo "same domain";
}Else{
Echo "not same";
}
}
https://3v4l.org/XtHWr

laravel vue multi language site : String as key [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 5 years ago.
Improve this question
I have been searching for an easy no plugin solution.
I am using laravel's string as key format for my translation file. I have fr.json file and inside this file I have all the texts and it's translations.
It works fine for blade but not being able to use it in my .vue files.
Please help me how can I use this fr.json file in all of my .vue file.
Thank you.
If you want to get your translation files in Vue, you'd have to import them in your javascript.
First. Set a meta tag in your head eith the current language:
<meta name='locale' content='{{app()->getLocale()}}' />
And then in your javascript for this example resources/assets/js/app.js:
var locale = document.head.querySelector('meta[name="csrf-token"]').content;
var lang = {
locale: require('../../lang/' + locale + '.json')
}
Maybe you can do the same for the default/fallback language. Then a translate function would look like this:
function trans(key, replace) {
var message;
if (lang[locale][key] != undefined) {
message = lang[locale][key];
} else if (lang[defaultLocale][key] != undefined) {
message = lang[defaultLocale][key];
}
if (message) {
// Loop through each item of replace and string replace the message.
return message;
}
return key;
}
Something like this could work I think. Didn't test it and needs some tweaking but I think that this should be the idea when solving this problem.

How to read data from the URL 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 7 years ago.
Improve this question
Just as an example:
http://domain.com/main.php(then the database file here).
I'm not sure what that is even called, but I can't find anything on it.
Of course you can use GET parameters
http://domain.com/main.php?foo=bar
Which you can get in PHP with:
$_GET['foo'];
I don't really know why you want to pass the database name in the link. Sure, it can be a parameter, but it so unsecure!
UPDATE: you may use more/multiple databases, but use then a config file. The connection names or database names do not passed in the url.
.../main.php?state=demo&type=1, .../main.php?state=demo&type=2, etc.
if ($_GET['state'] == 'demo')
{
switch ($_GET['type'])
{
case 1:
$databaseName = 'demo_type_1';
break;
case 2:
$databaseName = 'demo_type_2';
break;
default:
throw new Exception('Wrong demo specified');
}
// connect to database with the name in $databaseName
}
else
{
// Other connection
}
Better is to set demo state to session, so you can read out it without the url (more secure)
It is not recommended to put your database name in GET requests in the query string for security reasons. But, if you still need it you can do:
http://domain.com/main.php?database_name=MyDatabase
Then on the PHP Code:
<?php
$databaseName = $_GET['database_name'];
?>

Categories