click link and echo statement on next page? - php

Hi i'm trying to think of a way that if a user clicks a certain link it takes them to a new page where only if the user has come from that parent link it will echo a statement on the newly opened page?
Is this possible and if so does anyone know how i could do it? Thanks

I don't know if I understood your question correctly but give this a try...
HTTP_REFERER could help you do this but it isn't really reliable.
<?php
$referer = $_SERVER['HTTP_REFERER'];
$referer_parse = parse_url($referer);
if($referer_parse['host'] == "yoursite.com" || $referer_parse['host'] == "www.yoursite.com")
{
//from expected page
//echo here
} else {
//not from expected page
//do something else
}
?>
NOTE
This is a sample code showing the logic, you'll still need to modify this to fit your needs.
INFO
I would suggest trying to find a way/logic to implement it with the use of secret/session keys.
Good luck,
Madz

If I understood the question correctly
Link 1
Link 2
Link 3
Then in abc.php
$link = $_GET['link']; // Don't forget to sanitize the data
if($link == 1)
{
// user clicked Link 1
}
else if($link == 2)
{
// user clicked Link 2
}
else if($link == 3)
{
// user clicked Link 3
}
else
{
// came from somewhere else; i.e. did not click the links
}

Related

Five unique pages lead to one page. Can I change the <h1> according to the page they came from?

I have five unique forms each on a page of HTML. They then go to a PHP file to send the e-mail of data. Next they go to the HTML thank you page. I am hoping to change the heading according to which form they just submitted.
For example, if they submit a review, the should read "Thank you for your review" etc.
Technically all of these are saved as php files but only the e-mail page has php items.
Like <?php echo("<p>". $mail->getMessage()."</p>"); ?>
You should redirect to another php file and pass a parameter on url. Example:
sendemail.php
<?php
/** After send the email, check what kind form is (I don't know how do you check this).
This example is just to show you: */
if ($formType == 'review') {
$type = 'review';
} else if ($formType == 'anothertype') {
$type = 'anothertype';
}
header('Location: /thankspage.php?type=' . $type);
?>
thankspage.php
<?php
$type = $_GET['type'];
if ($type == 'review') {
echo '<h1>Thanks for your review</h1>';
} else if($type == 'anothertype') {
echo '<h1>Thanks for your anothertype</h1>';
}
?>
One way put a hidden field in your forms that'll get passed with the other form data. Then put an if statement on the thank you page and echo the appropriate message.
However, that'll only work either if you change the thank you page to php or change the page that receives and processes the form data to echo the thank you message as well

how to hide button for normal users and show for admin

I want to hide button in search menu to specific page for normal users so they can't access to page witch is for administrators only. i'm new at writing this code so i'm asking you guys who have much more knowledge than me for help.
I want to hide File/page name upload.php for normal users and show for administrators only
Is anyone know how could i do this with php?
i'm really appreciate for every help. Thank you!
You can capture user type of user's in session. And, according to user type show / hide button.
<?
if(S_SESSION['userType'] == 'Admin') {
//Show
}
if(S_SESSION['userType'] == 'User') {
//Hide
}
?>
It's hard to give an advice without your context, but an simple example for your scenario:
// Normal Page
if($_SESSION['user_level'] > x){
//show button
}
// Admin-Only-Page
if($_SESSION['user_level'] < x){
die("Access denied");
}
Depends of how you define the administrator, if he is the user whose the Id is 1 for example, use the following script:
<?php
$current_user_id = // get it from the session if user is logged in
If ($current_user_id == 1):
?>
<input ...>
<?php endif; ?>
For that you need to set one flag in Database as user is admin or normal user.
after that in your code check with condition that logged in user is admin or not;
for example admin role is 1 then:
if($user->role == 1){
// your button code
}
simple, Enjoy :)
for example, you can use following Conditional statement :
if ($user -> role == 1){
// `enter code here`
}

PHP registration form including the database

While I refresh my browser the entries of the registration form goes into the Database every time i press REFRESH, Professor told me to resolve this problem with the help of LAST_INSERT_ID().
I am able to get the last_insert_id from the database but doesn't know what would I do further with that ID.
Please help..
enter image description here
The recommended way is to use the Post/Redirect/Get pattern.
There are other ways to achieve what you desire here.
I am not sure what your professor is asking to do with the last insert id. Maybe he is referring to something like this,
if(isset($_SESSION['last_insert_id'])){ // At the beggining
//redirect to a another location
}
// Code for insertion goes here
$_SESSION['last_insert_id'] = $last_insert_id; // Get and store the insertion id as a session
I think you are using the same page to Save the Data, If it is So, then follow the following method :
<?php
if(isset($_POST[userName]))
{
// Put your Registration Operation Code Here
header('Location: ./Registrationform.php');
}
?>
After DataBase Insertion it redirects to the same page. Now Refresh is made with the GET Method not by the POST Method. So, you can eliminate the duplicate entries by this way.
As per your requirement I used the Last Inserted ID for the Validation before Inserting Records in the Database.
<?php
session_start();
if(isset($_POST[userID]))
{
$flag = false;
if(isset($_SESSION['last_insert_id']))
{
if($_SESSION['last_insert_id'] == $_POST[userID])
{
$flag = false;
header('Location: ./Registrationform.php');
}
else
{
$flag = true;
$_SESSION['last_insert_id'] = $_POST[userID];
}
}
if($flag == true)
{
// Put your Registration Operation Code Here
}
}
?>

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

Jquery over facebook , twitter , google plus iframe?

I want to run/execute jquery code on facebook, twitter, google plus iframe.
I have tried to do it in many ways but it is not working.
Code is always showing error none/object not found.
Actually I want to know whether currently logged-in user has already done like/follow/+ .
If the current logged-in user has already done like/follow then I need to hide like/follow links,
If you are using the PHP Facebook SDK you can query it as follows:
$likes = $facebook->api('me/likes');
$fanpageliked = false;
$appliked = false;
foreach ($likes['data'] as $ilike) {
if ($ilike['id'] == $fanpageid) {$fanpageliked = true;}
if ($ilike['id'] == $appid) {$appliked = true; }
}
If your fanpage id is stored in $fanpageid, or app id in $appid, then $fanpageliked will be true (and/or appid).
So if you are only looking for a fanpage id, remove the appid if and add a break:
if ($ilike['id'] == $fanpageid) {
$fanpageliked = true;
break;
}
After that, you can have the following:
if (!$fanpageliked) {
echo "Like us NOW!";
// .... display like button etc
}
else {
echo "You are ours now. Bwa ha ha!";
}
You can't do that with jquery but you can use their own APIs to accomplish what you need. Facebook for example already has all that in their API.

Categories