Get a variable from href with id - php

How can I get the id as a variable from that statement in same page?
I need to capture the #dialog to display in a popup and can't do that if I send the id to a different page.
Link
I've been using this to get it from the link but I need to capture it in same page. This is what i was using :
$img = isset($_GET['id'])

Everything after the Hash-Sign is evaluated by the browser only and not sent to the server. You therefore cannot access it.
Maybe you meant:
?id=<?php echo $image['image_id']; ?>#dialog
Otherwise you would have to use JS.

Related

Variable in string disappears when redirecting with header()

I have a SMARTY form and would like to pass a variable(set from the url/referrer) from the form page to the thank you page. So what I do:
I open my page with the form: example.com/index.php?variable=blabla
I get the variable and form the thankyou page URL
$urlconv = 'example.com/thankyou.php?variable=' . $_GET["variable"];
When the form is filled and submit is clicked the form redirects to the thank you page: header('Location: ' . $urlconv);
I even echo $urlconv on the first page to be sure that I've constructed the url correctly together with the variable. And it shows it correctly.
Unfortunatelly the redirect omits the variable for some reason. It goes only to example.com/thankyou.php?variable= for some reason...
Maybe by the time I call the redirect the variable is gone, so my question is can I somehow hardcode the variable in $urlconv? Because if the echo is showing it right and then the riderect omits it it means it has saved only a shortcut to the variable and not the actual value in the string, right?
I have very basic programming skills.
Thanks!
Try using the urlencode function. Along with that, if its within your browser, kindly omit the example.com and try using a relative link.
So
$urlconv = '/thankyou.php?variable='.urlencode($_GET["variable"]);
I've tried this right now, and it did work.
$urlconv = '/start.php?variable='.$_GET["variable"];
header('Location: '.$urlconv);
2nd solution: Try using javascript redirection. Also use double quotes while assigning $urlconv, as in $urlconv = "/thankyou.php?variable=".urlencode($_GET["variable"]);
<?php
echo "<script>location.href='".$urlconv."';</script>";
?>

check if a querystring exists, if not create one - PHP

I have several pages which use querystrings to highlight an option in a menu, all the url's on the page have the currant querystring phrased in them so the same menu option will be highlighted on the next page if the user clicks the link.
However the problem arrises when someone visits the page without the querystring included in the url, the menu option isn't highlighted.
What i would like to do is check the URL to see if a querystring is present, if one isnt, create one.
The url's are phrased as such www.mysite.co.uk/Folder1/Folder2/Page.php?id=3
and i would like the default querystring to be ?id=1 if one isn't already present in the url.
Any ideas on how you do this?
And what would happen if a user visits using the URL www.mysite.co.uk/Folder1/Folder2/Page.php?
Would the URL end up as www.mysite.co.uk/Folder1/Folder2/Page.php??id=1
or would it be www.mysite.co.uk/Folder1/Folder2/Page.php?id=1
Thanks,
Maybe there are plenty of ways. You can assign value to $_GET key if one does not exist. Or if you really need to query string, you can renavigate the user to the same page with present querystring.
if (!isset($_GET['id'])) {
header("Location: Page.php?id=1");
exit;
}
It should be before any output in the page. So if user visits Page.php or Page.php? or Page.php?someDifferentParamThanId=10 it will return false on isset($_GET['id']) thus it will redirect to Page.php?id=1
This should work:
if(isset($_GET['id'])){
//It exists
}else{
//It does not, so redirect
header("Location: Page.php?id=1");
}
Do something like:
if(!isset($_GET['id'])){
header('LOCATION:www.mysite.co.uk/Folder1/Folder2/Page.php?id=1'); die();
}
In php, the query string is loaded into $_REQUEST variable. In your case, $_REQUEST['id'] will be equal to 1, 3 or whatever you get in the query string.
For solving the problem when no id is given via GET, I think will be enough to add this line at the beginning of each php page:
<?php
if ( $_REQUEST['id']=='' ) {$_REQUEST['id']=1;}
?>
It is not necessary to change the URL on the fly.

ID variable from URL

Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);

How to check if a link has been clicked in php

Well I am new to this so I want to record when the user clicks on the link that php prints and query a mysql database. I know how to query the database using php already but I'm not sure if it is possible to know if the user clicked on the link.
I printed a link like so.
print ('<a id="myLink" href="http://www.google.com" target="_blank">google</a>');
To track the link, you'd need to create a link tracking script on your server. i.e. linktracker.php
Then, change your code to point the link to that script, passing the forwarding url i.e
<a id="myLink" href="http://mysite.com/linktracker.php?url=http://www.google.com" target="_blank">google</a>
In linktracker.php, you would need something like:
<?php
$url = $_GET['url'];
// update your database click count for the url
// i.e UPDATE linkclicks SET clickcount = clickcount + 1 WHERE url = '$url'
// forward the user to the end location
header("Location: $url");
You need to build a URL redirection mechanism.
$link = 'http://www.google.com';
echo '<a href="/redir.php?target="'.encodeUriComponent($link).'>google</a>';
then make a redir.php:
<?php
$targetUrl = $_REQUEST['target'];
// log this targetUrl to your MySQL database.
header( 'Location:'.$targetUrl);
I absolutely wouldn't do this in JavaScript if you want to try to track links shares or something of that nature.
add onclick="handleClick()" and write javascript function named handleClick to report the click to the server uaing ajax
Unless the link is to your own site, you'll need to use a client-side scripting language such as JavaScript (could utilise jQuery too) to send the user's click event back to the server.

How can I make a link load a random php ID on the page?

Database row IDs are tied to the URLs like this:
The page for row 2 becomes
site.com/index.php?id=2
If I manually refresh index.php it displays a random record, but naturally it doesn't display the record ID in the address bar.
Is there a way to make a link display a random record while showing the id change in the address bar?
Thank you for your help!
Instead of just showing the random record, choose the record's id and do a redirect to the version that shows the id.
$randomId = // whatever method you use to choose it
header( "Location: http://site.com/index.php?id=$randomId" );
There are multiple ways to do this.
You could like Juhana said, using the header function. This wil redirect you to another page by pagereloading.
You could use HTML5 to change the addressbar without pagereloading by using the window.history and jQuery for AJAX.
The safest option is the PHP header function. Keep in mind to NOT print/echo anything before you header. In case you still get "headers already sent" use ob_clean()
Try this :
$randomId = rand(0,100); // if let's say your post IDs go from 0 to 100
header( "Location: http://www.yourdomain.com/post.php?id=$randomId" );

Categories