How to read data from the URL in PHP? [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 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'];
?>

Related

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

How can I make a simple api that can connect to mysql? [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 7 years ago.
Improve this question
I need help on how to make an API for PHP. I was trying to make one web server communicate with another webserver through PHP.
I also want it to update MySQL code. I was using $_GET but it was less secure. Here is my code, can you please take a look at it?
<?php
/*
example: website-url-here.com/?command=insert-command-here&password=testing
*/
$command = $_GET["command"];
$password = $_GET["password"];
if ($password == "testing") {
//Was not a good idea, less secure.
//echo eval($command);
//More secure
if ($command == "create-user")
{
//create user command here
}
else if ($command == "delete-user")
{
//delete user command here
}
else
{
die("Command is incorrect");
}
}
echo "Success";
?>
This question is way too open ended to answer in a StackOverflow answer. Try reading up a little on REST, and a lot on PDO, especially in the context of sanitizing user input.
Think about what would happen if somebody called your api with [url]?command=rm -rf .&password=testing

PHP hand variable to another php document [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
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

Echo text to the user who submitted the form [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 have a 2 file PHP script which allows a user to submit a form with a single text area. This script creates a new PHP page and redirects the user to it. I would like to echo text to the user who submitted the form and no one else. Can you help with a code example?
let me know if this isnt what youre looking for and ill edit it accordingly:
HTML:
<input type = "text" name = "input">
PHP:
<?php
//user input
$userInput = $_POST['input'];
//not sure how you are getting the email adress but however youre getting it it goes here
$email = "placeholder#123.com";
//create the filename
$myFile = $userInput . ".php";
//create the file
$fh= fopen($myFile, 'w');
//write to the file
fwrite($fh, $email);
//redirect to the new file
header("location:". $userInput .".php");
?>
basically it creates a new page based off of whatever your user entered and displays an email address to them. I suppose it kind of only allows the user to see it, as it is based off of what they type. other wise the best way to do this would be to store a cookie on the users system (as far as the security goes)

Categories