PHP: How can I retain and use link parameters - php

I am currently trying to work out a basic referral form. The process is as follows:
The referrer will use a form to send an email to their friends. (Example: http://www.graphicgoldfish.com/refer.html) As you can see, the form requires their Username and their IGN (in-game name). This is very important.
A link is generated using the referrer's information as the parameters. The parameters will be used to reward the referrer once the friend has completed the second form. (Example: http://www.graphicgoldfish.com/referral.html?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish)
When that link is clicked, it opens up a second form where the friend can input their information. My problem is, that when the friend submits the form, the parameters from the original URL do not get passed.
How am I able to keep those parameters after the submission?
My PHP:
<?php
echo $_GET['ref_username'];
echo $_GET['ref_ign'];
if (isset($_GET['ref_username'])) {
$r_username = $_GET['ref_username'];
}
if (isset($_GET['ref_ign'])) {
$r_ign = $_GET['ref_ign'];
}
None of this seems to be doing what I want. My guess is that it doesn't exist, because if I run the PHP and manually add the parameters, it works. I'm just not sure how to go about doing this automagically.
EDIT: The parameters are being passed into an HTML file from the link that was generated. The friend needs to fill out a form. How can I get the values that were passed in the parameters to the HTML file, and concatenate them to the action (where it submits to a PHP file)?
This is not working for me.
<form action="http://www.graphicgoldfish.com/php/referral.php?ref_username=<?php echo $ref_username ?>&ref_ign=<?php echo $ref_ign?>" method="POST">

First off...in your generator.php form. You are using POST not GET
So you should retrive your variables as such.
if (isset($_POST['ref_username'])) {
$r_username = $_POST['ref_username'];
}
if (isset($_POST['ref_ign'])) {
$r_ign = $_POST['ref_ign'];
}
Secondly when you generate that link with the paramters in it. You are passing the parameters to an html file.
http://www.graphicgoldfish.com/referral.html?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish
You need to pass them to a php file for this to work...
http://www.graphicgoldfish.com/referral.php?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish

Related

Different profile page for different users using PHP

Hey I'm new to php need help creating anew page per user. I have a user login and registration system already. I also have a profiles.php page but, how can I let the website make an automatic webpage for every new user.
Whenever I try to connect it through $_GET or $_POST I get an Undefined index error.
include ("includes/profiles.dbh.inc.php");
$requested_user = $_POST['mailuid'];
try{
$stmt2 = $conn2->prepare("SELECT * FROM profile WHERE id = ?");
$stmt2->execute(array($requested_user));
$mydata = $stmt2->fetch();
} catch (Exception $e) {
//error with mysql
die();
}
I've seen this question come up a few times. One of the first things you need to consider is how is the user going to interact with this page.
Are they going to access it via links? http://example.com/index.php?user=1
Or are they going to submit a form via post? http://example.com/index.php
This distinction is important because it changes the way you handle their request.
Let's look at each one:
Via a link: http://example.com/index.php?user=1
This will issue a get request to your server so you need to handle it via $_GET.
if (isset($_GET['user'])) {
$userId = $_GET['user'];
// ... query your DB and output result
}
Via a form using post: http://example.com/index.php (body with contain "user=1").
This will issue a post request to your server so you need to handle it via $_POST
if (isset($_POST['user'])) {
$userId = $_POST['user'];
// ... query your DB and output result
}
On a side note, it is important to know that an HTML <form> can submit either a post or get request. You can control this via it's method attribute. If you don't specify the method attribute, then it defaults to get.
<form method="post">
...
</form>
<form method="get">
...
</form>
So, to answer your question. You're likely getting an undefined error because you're trying to access the post array using the key mailuid ($_POST['mailuid']) but that does not exist. It doesn't exist because:
you're receiving a get request and the post is empty OR
you are receiving a post request but it doesn't contain the key mailuid
To debug, go back to the basics - use $_GET
Change your code to use $_GET['mailuid'] and then make sure you access your page with the corresponding query string - ...?mailuid=1.
Finally, turn on error reporting - https://stackoverflow.com/a/5438125/296555. You should always do this and correct all errors and warnings.

how to check if bookmarks exist in html document

I've just designed my first form in HTML and a PHP page to display the results. In the form the user inputs some codes in response to some questions, a bit like a multiple choice, so for example, these are "ABC". The PHP page displays the code to the user as a link, which when clicked will go to a bookmark (a link within the same page) with the ID #ABC. This was achieved with simple manipulation of the PHP variable as follows:
<?php
$code = "ABC"
$part1 = '<a href="mywebpage.php#';
$part2 = '">Go to this code</a>';
$string = $part1.$code.$part2;
echo $string;
?>
(i.e. Link in the page says "go to this code" and when clicked will go to section with bookmark ABC)
This all works fine, but I simply need to know if there is a way of error trapping so that if a bookmark does not exist for the code entered, a message can be displayed to the user instead? Can this be done using the PHP variable, or do I need to use JavaScript? One work around may be to search the web page for the ID "#ABC'. Is it possible to do this? Another option would be to store an array of valid codes on the server then query this before setting the bookmark, but I want to keep it as simple as possible. Any help appreciated, thanks.
What you call a "bookmark" we call a hash. And when you say "go to a bookmark" you mean a hash change. Hash changes do not make an additional request to the server, it is all handled on the client-side, therefore this must be done with JavaScript and not PHP.
So let's just do some simple JavaScript on hash change window.onhashchange that will search for an element with that ID and if it's not found alert something.
window.onhashchange = function(){
if(!document.getElementById(location.hash){
alert("not found");
}
}

deleting a file from the data base by clint

function appoint_del(sat,sat1)
{
if(confirm("Are You Sure To delete Selected Person Details Completely?"))
{
document.form1.action="student.php?cedit="+sat+"&did="+sat1;//
document.form1.submit();//an alternative to call form
}
}
<?
if($_GET['did']!="")
{
$del=executeupdate("delete from table2 where id=".$_GET[did]);
redirect("student.php?succ=3");
}
?>
To delete the content in data base by clint I have successfully did the job but I am not completelly aware of what is happining by the statement1 document.form1.action="student.php?cedit="+st+"&did="+st1;
and statement2
document.form1.submit()
can any one explane it?
and can sugest any good reference book for clarifing these type of doubts?
document.form1.action="student.php?cedit="+sat+"&did="+sat1;
This line of JavaScript sets a html form's action to the student.php page and appends the two parameters "cedit" and "did" along with their values. I assume you have a form on your page somewhere.
document.form1.submit();
This submits that form. I don't know where to or by what method (POST, GET) because you haven't provided any details on the form. I assume it submits back to the same page because the next line is a handler:
if($_GET['did']!="")
This detects whether the form was submitted by checking if the "did" parameter is present.
$del=executeupdate("delete from table2 where id=".$_GET[did]);
This seems to execute a database query with a massive SQL injection vulnerability. Very dangerous.
redirect("student.php?succ=3");
Redirects back to the same page again, this time passing in a different "succ" parameter which I assume is handled via some other code that you haven't provided.

DELETE http method with REST API using PHP

I'm working on making a small application just to get the hang of REST and I'm having some trouble with the DELETE method. I've never used it before so I'm not sure how it behaves.
Anyway, I followed this tutorial to get a hang of the basics. I modified it so instead of using a pre-made array, the user can submit data and view it from a MySQL databse.
I have 3 files:
server.php - the "API" which determines the method used and acts accordingly
input.php - displays a form for the user to input data
viewinput.php - displays the inputs that have been entered.
I'm trying to now place a "delete" button on viewinput.php so that an entry can be deleted. Here's my code that displays the entered information.
while ($result = mysql_fetch_array($sql)){
?>
<tr><td><? echo $result['id']." "; ?></td><td><? echo $result['text']; ?></td>
<form method = "delete" >
<td><input type="submit" name="delete" value="delete"></input></td></tr>
<input type="hidden" name = "hidden_delete" value="<? echo $result['id']; ?>"></input>
</form>
<?
}
Now, in my server.php file (the API), this is the very first function that is called which determines the method and breaks the URL into components for further processing.
public function serve() {
$uri = $_SERVER['REQUEST_URI'];
echo $method = $_SERVER['REQUEST_METHOD']; //GET and POST are displayed, DELETE isn't
$paths = explode('/', $this->paths($uri));
array_shift($paths); //
$resource = array_shift($paths);
When I press a delete button, the URL goes from
/rest/viewinput
to
/rest/viewinput?delete=delete&hiddendelete=3 //assuming I deleted the 3rd entry
From what I understand, the URL should be /rest/viewinput/3 when a DELETE method is submitted
In my server.php file, when I echo the method, "DELETE" isn't shown like it does for POST and GET methods.
I found this resource regarding DELETE, and from what I understand an identifier will be passed through the URL, but there should be some method received just like GET and POST (meaning my code should display DELETE when I echo the method).
Here is the current lowdown on PUT and DELETE.
If you are "plain browsing", most browsers don't actually use either of these verbs. They do however support them for XML Http Requests (i.e. AJAX). So from the client side, you can't really use PUT or DELETE directly on an HTML form element and have it work. You would need to use the work-around suggested by Complex857, i.e. include the intention in the request.
If you are using background AJAX calls, or indeed if you are not using a browser to make the requests, you can use the full suite of HTTP verbs, including PUT and DELETE. One caveat to this is that on IIS you may need to change your settings to allow these verbs as you can set the specific verbs your server accepts.

HttpWebRequest POST and retrieve data from php script after login

Hello guys i am newbie to this stuff so i'll try to explain my problem.I am building application that retrieve data after login to php script that looks like this:
https://zamger.etf.unsa.ba/getrssid.php
(see the page source for php scirpt definition)
and definition(source) here:
Korisničko ime (UID):
Šifra:
After i login it shows me data that i must collect like this:
RSSID: 1321B312 (this is only data that it shows and nothing else)
I must do this with httpwebrequest but don't know how i tried to do it with POST(data) but it always give me the defnition of php script as response.But i need response to be like "RSSID: 1321B312" not as script definition mentioned above...please heeelp ASAP....
Define a form action to begin. So if the same page, getrssid.php, will be processing the form, use:
<form action="getrssid.php" method="POST">
After that, you must code getrssid.php to accept the incoming data via POST. POST data is stored in the PHP variables $_POST['inputname']. So if you have the input name as "login", the value entered will be stored in $_POST['login']. Same thing applies for the password.
So, here's a sample of what a basic POST data handling script should look like. Note that this script does not verify the login credentials, sanitize the inputs, or anything of the sort. It is just to show you how to handle POST DATA.
<?php
if (isset($_POST['login']) && isset($_POST['pass'])){
// Form is submitted.
echo 'RSSID: 1321B312';
} else {
// Form is not submitted.
// Display Form.
echo 'Form HTML here';
}
?>
If you are really server conscious, you should put the if ... else statement in the opposite order so the most likely outcome (form not submitted) is evaluated first.
Merry Christmas!

Categories