So, im making a file hosting site, and am using some form builders to start off with. However, these builders do NOT support PHP. Now, i would like to shrink some URLs, how can i do this in pure HTML, without adding in PHP methods. I am fine with goo[dot]gl, bit.ly, tinyurl.com, or whatever else!
HTML is a Markup Language.
If you want to use some API, or anything more coding-oriented, you have to use a real programming language - you choose : for your purpose PHP would be the best choice.
Now, if you finally decide to use PHP, it's really easy.
Code (for TinyURL) :
<?php
function createTinyUrl($strURL) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$strURL);
return $tinyurl;
}
?>
Usage :
<?php
$myTinyUrl = createTinyUrl("http://www.yourdomain.com/some-long-url-here");
?>
And that's all! ;-)
If the form builders don't support PHP you need to write it yourself. PHP is very easy to work with.
Here is an example for you. (Assuming you have PHP set up on your web host:)
Save the file with the extension .PHP (or whatever your web host uses - might be .PHP5 for php5) instead of .HTML
You can use the super-global $_GET to accept certain variables from the URL in the address bar ex.:
$short_url = $_GET["q"];
Since i'm getting a variable named 'q', if you access the page with a parameter named 'q' I will have that variable stored ex.:
http://your.site/?q=shorturl # Assumes your index file takes the 'q' variable
Now it is up to you what to do with that variable. The best thing would be to set up a MySQL database so that when you get a value like 'shorturl' you can do a quick SQL query to return the full address ex.:
# Make DB connection
$db = new PDO("mysql:host='X.X.X.X';dbname='mydb'", $user, $secret);
# Function to search database for URL
function getFullURL($short_url) {
$result;
$sql = "SELECT full_url FROM tbl_addresses WHERE short_url='?'";
$query = $db->prepare($sql);
$query->execute(array($short_url));
$rows = $query->rowCount();
if ($rows == 1)
$result = $query->fetchAll();
return $result;
}
There's really not much to it in PHP with MySQL.
Related
Just a small question for anyone out there that uses smarty. I am trying to pass PHP directly into my code, but when I do, the cached version cuts out the PHP and just prints it directly like so.
<div class="dashboard-card-content">
<?php
$con = mysqli_connect(Nice,Try,Fly,Guy);
$company_id = $_smarty_tpl->tpl_vars['auth']->value['user_id'];
$company_id = mysqli_query($con,"SELECT company_id FROM cscart_users WHERE user_id = $company_id")->fetch_object()->company_id;
$company_id = mysqli_query($con,"SELECT goal FROM cscart_companies WHERE company_id = $company_id")->fetch_object()->goal;
echo "Your current goal is: ".$company_id;
?>
This just prints all of it out on my webpage, so I tried using the following:
{Literal}
{Include_php}
{php}
And I just can't find a way to get my PHP code to go into my TPL how I want it. This is becoming really frustrating and all I want is for my cache files to leave the PHP code alone. Sorry if this is a dumb question but I have been researching this for a while. How do I implement SmartyBC so that I can still use PHP injections. And if using SmartyBC is a bad idea, can someone give me a dumbed down version of how to use a seperate PHP function page to set variables to show in the Template?
Smarty is a template engine for presentation logic only. You cannot put application logic inside a template. It was possible in older versions of Smarty but fortunately not anymore. Just execute those funcions in a php file and pass the result to the template.
And yes, you can use SmartyBC: http://www.smarty.net/docs/en/bc.tpl, but that's supposed to be used for compatibility with existing projects. It's a really bad idea and shouldn't be used for new projects.
Why do you want to use php in Smarty?
Put your logic into a class or function, and pass the data via the controller: Registry::get('view')->assign('smarty_variable', $data), and you are good to go.
You can create PHP function which gets necessary data from database. E.g.
function fn_get_company_goal($user_id)
{
$company_id = db_get_field("SELECT company_id FROM ?:users WHERE user_id = ?i, $user_id");
$goal = db_get_field("SELECT goal FROM ?:companies WHERE company_id = ?i, $company_id");
return $goal;
}
Put it to your addon. Then you can use it in the Smarty template in the following manner:
{$goal = $user_id|fn_get_company_goal}
for security reasons we need to disable a php/mysql for a non-profit site as it has a lot of vulnerabilities. It's a small site so we want to just rebuild the site without database and bypass the vulnerability of an admin page.
The website just needs to stay alive and remain dormant. We do not need to keep updating the site in future so we're looking for a static-ish design.
Our current URL structure is such that it has query strings in the url which fetches values from the database.
e.g. artist.php?id=2
I'm looking for a easy and quick way change artist.php so instead of fetching values from a database it would just include data from a flat html file so.
artist.php?id=1 = fetch data from /artist/1.html
artist.php?id=2 = fetch data from /artist/2.html
artist.php?id=3 = fetch data from /artist/3.html
artist.php?id=4 = fetch data from /artist/4.html
artist.php?id=5 = fetch data from /artist/5.html
The reason for doing it this way is that we need to preserve the URL structure for SEO purposes. So I do not want to use the html files for the public.
What basic php code would I need to achieve this?
To do it exactly as you ask would be like this:
$id = intval($_GET['id']);
$page = file_get_contents("/artist/$id.html");
In case $id === 0 there was something else besides numbers in the query parameter. You could also have the artist information in an array:
// datafile.php
return array(
1 => "Artist 1 is this and that",
2 => "Artist 2..."
)
And then in your artist.php
$data = include('datafile.php');
if (array_key_exists($_GET['id'], $data)) {
$page = $data[$_GET['id']];
} else {
// 404
}
HTML isn't your best option, but its cousin is THE BEST for static data files.
Let me introduce you to XML! (documentation to PHP parser)
XML is similar to HTML as structure, but it's made to store data rather than webpages.
If instead your html pages are already completed and you just need to serve them, you can use the url rewriting from your webserver (if you're using Apache, see mod_rewrite)
At last, a pure PHP solution (which I don't recommend)
<?php
//protect from displaying unwanted webpages or other vulnerabilities:
//we NEVER trust user input, and we NEVER use it directly without checking it up.
$valid_ids = array(1,2,3,4,5 /*etc*/);
if(in_array($_REQUEST['id'])){
$id = $_REQUEST['id'];
} else {
echo "missing artist!"; die;
}
//read the html file
$html_page = file_get_contents("/artist/$id.html");
//display the html file
echo $html_page;
I am working a website for my friends and myself, and it needs to be a bit secure.
I have an SQL database for storing user info,
I store the variables for the database outside the public files and have
a function to retrieve them.
With this function which is called getSQL_Info($n) it takes the
line in the text file and breaks it up and puts the
variables into an array and returns array[$n].
In order to load the file with the variables I must provide the location, instead of having the path written in the function, I would like to have it so you do something like
$_SERVER['DOCUMENT_ROOT'] and use that to get the base part of the path.
Essentially, I would like to have the path "home2/(mySecretUsername)/config/file.txt"
when it gives me "home2/(mySecretUsername)/public_html/fpi".
I kinda just need a good explanation of how to take the "home2/(mySecretUsername)/" part out and add "config/file.txt".
Thanks for your time,
if I explained my question poorly please do tell me and I will add further information.
-Michael Mitchell
Would this approach work for you?
function getConfigFile() {
$components = explode('/' , $_SERVER['DOCUMENT_ROOT']);
$componentLen = count($components);
if ($componentLen < 3) {
return ''; // Something goes wrong
}
$components2 = array_slice($path, 0, $componentLen - 2);
return implode( '/' , $components2) . '/config/file.txt'
}
It's obviously platform-dependent but looks like it does the job.
I am new to PHP.
I need a help regarding the methods of extracting DB name and table name from the given URL name.
For example, let's say, I have an URL like the one below:
/test.php?db=...&table=.../
How to extract the DB name and table name from this URL using PHP and use the result for other query purposes.
If you mean how to parse an existing URL for it's parameters:
parse_url() and parse_str() will help you strip the components of the url. You will primarily be looking at the following
$elements = parse_url($url);
$kvps = $elements->query;
$db = parse_str($kvps['db']);
$table = parse_str($kvps['table']);
But, if you mean how to GET variables from the current page before render:
<?php
$dbname = $_GET['db'];
$tablename = $_GET['table'];
?>
And yea, there are major security risks involved in opening up 'direct' access to your database this way. Best to obfuscate / encapsulate / wrap your functions in tasks like index.php&addUser=tim instead of index.php&insert=tim&db=boofar&table=users&dbuser=root&dbpassword=secure.
If you're just learning, what you're doing is fine, as long as you realize why it's wrong. If you're coding for production, you really need an alternate solution.
There are two ways to pass variables or data to another page.
GET (via the URL)
and
POST (usually a form submission)
You can alway get via
$_GET
http://php.net/manual/en/reserved.variables.get.php
or
$_POST
http://nl.php.net/manual/en/reserved.variables.post.php
I am currently working on a PHP script that will be polling Active Directory to pick out modified objects (people/users), via LDAP.
I'm able to filter on uSNChanged when I have the value, like so:
$previousUsn = '1234';
$ldapCon = ldap_connect('ldap-host');
$ldapBind = ldap_bind($ldapCon, 'ldap-user', 'ldap-password');
$sr = ldap_search($ldapCon, "ou=Users,dc=foo", "uSNChanged >= $previousUsn");
According to this, I should be able to retrieve a highestCommittedUSN attribute that could be used for the initial run of the script. I've been looking around to find out how this can be done using PHP & LDAP, but to no avail.
Alternatively, feel free to suggest completely different methods of retrieving changes in AD.
ldap_read(...) seems to do the trick:
function getHighestCommittedUsn() {
ldap_bind(...);
$sr = ldap_read($ldapCon, null, "(highestcommittedusn=*)", array("highestcommittedusn"));
$rs = ldap_get_entries($ldapCon, $sr);
return $rs[0]["highestcommittedusn"][0];
}
Try setting the search base (second argument) in your ldap_search call to "". That attribute is on a pseudo object called RootDSE.
Perhaps if the biffins function hasn't been initialised correctly? Hmmm try changing the variable type of c_willygham