Permalinks with PHP - php

I am currently working on a website I am making from SCRATCH :) Anyhow users will be able to create accounts (login and register pages already done), and then they will be able to view there profile. Now the problem I am running into is each user needs there own permalink for there profile's URL. I do not know how to go about doing this though! :( All I can find is using Wordpress to do it for me, but I do not want that. I would like to go about doing this with only PHP and HTML/CSS. Please help me out, thank you! :)
UPDATE 1
Thank you for the relpys, I am still a little confused though. Now if I use a GET, will that actually save the file? How exactly do permalinks work (sorry very confused! :O )? Like when I create the permalink is that creating a new file as well with the code inside of it? If so what would I be writing for that?
UPDATE 2
Sorry I am specifying what I am really asking. When a user created a account, I want it to AUTO create them a permalink (which would be a new file right?), how would I go about doing that?
UPDATE 3
Ok, so I am basically thinking of the same thing. So when a user creates a account this is the code that is executed:
//Create Account Page
$inputquery = "INSERT INTO users username VALUES :username";
$datasend = $connection->prepare($inputquery);
$datasend->execute(array(':username'=>utf8_decode($username),)); //Inputs it into the database
$username = $_POST["username"]; //The username input, then lets say that the account was created with that
$userprofile = fopen($username . ".php", "w"); //Makes a file with the users info, a PHP function
$text = include ('base.php'); //Grabbing the base layout
fwrite($userprofile, $text); //Setting the base layout to the userprofile page
So once that is one the code will be in the file. Then when the page is loaded, that so called "base" code is still there, and then is replaced with the details of the user. That is my rough attempt at this, it should work! :) Thank You for all of your help! :D

You can use a GET request, which will be stored in the URL. You just access it with $_GET['user'], and the url will be in the form my.site.on.the.net/user.php?user=3141. This URL can be stored in bookmarks, put in a webpage, or whatever else and it will link to that user's profile.
This will not save the file by itself. You will need to store the user information somewhere outside your web root, then use the PHP file to access that info based on the ID. You don't want to store the entire webpage; just store the information that is unique to that user (On SO, that would be stuff like their About Me, reputation, badges, etc.)
EDIT: Simple example (note that it doesn't have any password protection or anything else to prevent any random stranger from accessing it)
user.php:
<?php
function getPoints(){
// get the number of points from your data source
}
function getName(){
// get the name from your data source
}
?>
<!DOCTYPE html>
<html>
<body>
<img src="logo.png" alt="logo" />
<h1>Hello, <?php echo getName();?>!</h1>
<p>You have <?php echo getPoints();?> points.</p>
</body>
</html>
In this example, the data source only needs to store the user's name and number of points. The single PHP file fills in the rest of the page. To create a new user account, you will need to add the data for the user to your data source.

Related

How to create iframe with a dynamic url using user's data

First time posting a question here. I'm definitely NOT a coding expert, I have learned some HTML and CSS thanks to Google, StackOverflow, and lots of trial and error, but this one seems to be a PHP issue, and I know almost nothing about PHP.
I am creating a subscription Wordpress website and I need to create an iframe with a dynamic src value.
The first part of the src value would be the same for every user (for example https://app.example.com/autoin/) and then it should be completed by a unique code given to each user. That code lives in one of the fields in the user's profile. I'm currently using the second address line for that. The id or name for that field is address-two.
The closest I have come across to a working code is the one below, which I add to directly to the page I will use, using a Raw HTML box from WPBakery.
<?php
$current_user = wp_get_current_user();
if($current_user) {
?>
<iframe class="metricool-iframe" src="https://app.example.com/autoin/<?php echo $current_user->address-two; ?>>
</iframe>
<?php
}
?>
When I check the View Page Source in Chrome, I can see that the URL is still https://app.example.com/autoin/<?php echo $current_user->address-two; ?>
So, it's not really working.
Please, is there anything I can do to improve the code above. Or is there any other method? Please keep in mind that I'm a total beginner to this :P
Thank you in advance!!
Ern.
You could try this:
<?php
$current_user = wp_get_current_user();
if(isset($current_user)) {
$useraddress = $current_user->address-two;
echo "<iframe class='metricool-iframe' src='https://app.example.com/autoin/".$useraddress."'></iframe>";
} else {
echo "No User Set!";
?>
If this helps please mark as answer, it would mean a lot!
EDIT:
What is the name of your file? (Ex: index.html, index.php)
And what webserver are you using, and are you 100% sure it has php running on it?
It's a Wordpress website, the index page is index.php.
I checked my server with https://iplocation.io/ and it returned "Apache". Not sure if that is what you were referring to.
I was wondering if, instead of generating the URL with PHP, I just put the full unique URL in the address-two profile field?
I tried this, but it doesn't work:
<?php
$current_user = wp_get_current_user();
if(isset($current_user)) {
$useraddress = $current_user->address-two;
$useremail = $current_user->email;
echo "<iframe class='my-iframe' src='.$useraddress."' email='.$useremail."'></iframe>";
} else {
echo "No User Set!";
?>
Also, note that I will need to request the user's email address too and add it as a parameter.
Thank you very much!

How can I stop myy query in URL is from being lost after loading the page

I have an affiliate program and have a serious problem that I can figure out.
My affiliates have links like this...
http://example.net/?p=14&ref=delta88
Once the page loads it changes to...
http://example.net/?p=14
Which totally gets rid of the ref id. I need it to keep the whole URL in the bar in case they hit refresh. Because when you hit refresh it takes the affiliate out of the system and just let's people join without an affiliate.
The way my code works for the pages is this...
That URL goes to an index.php file. In that file it finds all the affiliates information. It then uses an include to show the page. So it's not pointing directly to the page. I need to use the include because I store about 27 pieces of data in strings and I can't put that information in a URL as queries and have it forward to that page.
I added that information because it may be because of the include that's causing it and that will help you better figure out a solution for me.
Use a SESSION, its like a variable that holds for each user, here is a tutorial but works like:
<?php
session_start();
if(isset($_GET["ref"]){
$_SESSION["ref"] = $_GET["ref"];
}
?>
Now, in any PHP that open the user, will have that variable set ( $_SESSION["ref"])
you can keep current url in variable , see below used actual_link to hold the current url data.
if($_GET){
$actual_link = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//if you want to redirect page on same url just call header with $actual_url
header('Location: '. $actual_link);
}

Can a Directory alone load specific query on a page?

On websites such as facebook and many others you see URLs such as www.facebook.com/username. How does a URL such as this actually load the users information from a MySQL database? and what is the actual file it is displaying on? I would assume there's not really a folder for each user that it is referring to. If my question doesn't make sense can you at least point me in the right direction to set something like this up through PHP?
Again, I want example.com/username to load my users profile. How does this work?
By using apache's .htaccess file to manage a RewriteEngine, all of your pages can be funneled through an index.php file. After confirming that the requested page is not actually a page that you've intended to be a default part of your web page, you can fall back on the code below, to discover a user account. If a user account is not discovered, then the likelihood is that the page being accessed is simply a 404, which you could then redirect to as a catch-all scenario
.htaccess file
RewriteEngine on
RewriteBase /
RewriteRule !\.(xml|js|ico|gif|jpg|png|css|swf|php|txt|html|otf)$ index.php
php logic to run after confirming the requested page, isn't something like a contact-us page, or any typical web page an end user would be attempting to access.
if(preg_match("/^\/(?P<username>[^\/]*)/", $_SERVER['REDIRECT_URL'], $matches)) {
$result = mysql_query("SELECT * FROM users WHERE username = '" . mysql_real_escape_string($matches['username']) . "'");
if($user_record = mysql_fetch_row($result)) {
echo "DO WHATEVER YOUR HEART CONTENTS HERE :)";
} else {
header("Location: error-404.php");
}
}
It is all loaded dynamically via the database. For example, my Facebook name is "benroux". In facebook's user table, there is going to be a unique column called, lets say, nickname. When I visit Facebook, they are parsing the path info and essentially calling a query in the form:
select * from user where nickname = "{$nickName}"
Now, this is an over simplified example, but I hope it gives you an idea of what is going on here. The key is that there are 2 types of url vars, the facebook.com/pagename.php?id=blah and the restful style path info vars facebook.com/pagename/var1/var2/
If you are looking to have example.com/benroux load my user page, you need to make your index.php (I'll use PHP) load the path info ( http://php.net/manual/en/function.pathinfo.php ) and then query the database as I have described above.
try to
print_r($_SERVER);
you will get that parameters. Then you just need to split them.
Something like
$directory = $_SERVER['REQUEST_URI'].split('/')[1];
So, put $directory into query

PHP - Use GET to include cms (above web root), how to include other files from the cms index?

I am trying to hide my websites cms application...
So i thought i would add a bit of php to any random page on my site, that includes a GET referance to some random string... So basically, if you go to x page, and add ?RANDOMSTRING the cms index is included. This is stored above the web root... Here is the peice of php:
if (isset($_GET['J7sd-H3sc9-As3R']))
{
require_once($docRoot . '/../../includes/admin/index.php');
}
Basically, index.php is laid out as a page with 3 fieldsets. In the 3 field sets are various links relating to various applications that deal with various tasks. They were accessed through the same means as the above code. And they were held in the web root and were able to be accessed via http...
That all worked perfectly fine, But the problem now comes when i try to access any specific part of the cms...so what would have been:
http://www.mysite.com/admin/part/
is now:
include($_SERVER['DOCUMENT_ROOT'] . '/../../includes/admin/part/index.php');
Or something of the sort...
So now when i go to my page at
http://www.mysite.com/randomDirectory/
and add:
http://www.mysite.com/randomDirectory/?J7sd-H3sc9-As3R
I get sent to my cms... Cool... But when i try to click on any section i get this header:
http://www.mysite.com/randomDirectory/?part
and the page gets refreshed to:
http://www.mysite.com/randomDirectory/
If that makes sense...
Could any provide me with any input or suggestions regarding the task that i am trying to accomplish? I am not sure if it is even possible to start off with, but it seems simple enough.
Any replies would be greatly appreciated, Thanks!
I guess you should append at the end of every link in your page something like
<?php if (isset($_GET['J7sd-H3sc9-As3R'])) echo '?J7sd-H3sc9-As3R'; ?>
Example:
http://www.mysite.com/randomDirectory/randomPage<?php if (isset($_GET['J7sd-H3sc9-As3R'])) echo '?J7sd-H3sc9-As3R'; ?>
edit
An easier way to do this would be to use sessions, in this way:
<?php
session_start();
if (isset($_GET['J7sd-H3sc9-As3R']))
{
$_SESSION['token'] = 'J7sd-H3sc9-As3R';
}
if (!isset($_SESSION['token']) || $_SESSION['token'] !== 'J7sd-H3sc9-As3R')
{
exit;
}
// go on with your page
?>
In this way, when you open a page with your token in the url, the session is started and the token is saved in the session, so it should work without the need to insert the token in every url until you close your browser.

how can I restrict user access on specific pages? (Drupal, php, FileMaker Pro 10)

I am working on a website which already has user access set up so that only members who are logged in can see certain pages. Now i need to go in and make the access for logged in users more specific. So, anyone who logs in can see the site, but there are 2 pages (called PDQ and Comm Plus) that should only be accessed by users that have PDQ or Comm Plus access. I use a filemaker 10 database to keep track of user information. If the user has PDQ access, then PDQ=1 in the database. the website is coded in Drupal,php, and html.
So right now I have a good idea of what to do but there are some pieces missing and im not very familiar with drupal or filemaker. I need for the page to get the user information and see what the value of PDQ is in the database. If 1 then grant access to the page, and if 0 go to the access denied page.
Here is what I have so far
<?php require_once('DatabaseName');
global $user;
//looks at the current user
$use = $user->uid;
//Not sure what goes here. I need code that looks at the filemaker database to see
//what the value of PDQ is and then stores it in a variable.
if (!session_id()) session_start();
if (!isset($variableGoesHere) || $variableGoesHere == '0'){
drupal_goto("access-denied");
exit();
}
?>
Any help would be greatly appreciated. Also, let me know if im on the right track or if I need to be looking somewhere else. Thanks!
First of all, using Drupal and not using the Drupal system to store info is a bad idea. If you use Drupal, you can use an external source to do the login, but your users should have a drupal user.
When your users are Drupal users things get a lot easier. You can use the Drupal access control system to check access etc.
If Drupal is serving the page, you should never write code like you have shown, hook_menu which is how you register paths has a access callback option, where you can handle your access, or you can just check if the user has a permission. This stuff only applies if you are doing the stuff in a custom module, which is what it seems like you are doing.
In any regard you should use the drupal_access_denied function if you want to return access denied yourself.
I figured this out a long time ago, but I never got around to answering the question. So heres what I did
$WebAuth_find = $FILMAKER->newFindCommand('WebAccess');
$Search_findCriterions = array('Access::cntdPhoneNumberDisplayCalc'=>"==".$find,'Access::phoneType'=>"E-mail",'Access::phoneMain'=>"==1",'LoginAccess'=>'1');
foreach($Search_findCriterions as $key=>$value) {
$WebAuth_find->AddFindCriterion($key,$value);
}
$WebAuth_Result = $WebAuth_find->execute();
if (FileMaker::isError($WebAuth_Result) && $WebAuth_Result->code == 401) {
echo "FM ERROR CODE: ".$WebAuth_Result->code."<br>"."ERROR: ".$WebAuth_Result->getMessage();
This identifies the current user in the Filemaker database under the WebAccess layout. It throws an error message if there is a problem. Now that I am looking at the current user I have it look to see what is in the Comm Plus and PDQ fields in the database, and create a session to hold the information.
else{
$FinalResult = current($WebAuth_Result->getRecords());
$_SESSION['district']= $district;
$PDQ = $FinalResult->getField('PDQ_subscription');
$_SESSION['PDQ'] = $PDQ;
$CommPlus = $FinalResult->getField('CommPlus_subscription');
$_SESSION['CommPlus'] = $CommPlus;
Then I just add the following code to the top of whatever page I want to restrict access to. It looks at the session to see if the current user has credentials if not they are directed to the access denied page.
$PDQ_check = $_SESSION['PDQ'];
if (!isset($PDQ_check) || $PDQ_check == '' || $PDQ_check == '0'){
drupal_goto("access-denied");
exit();
}

Categories