How to use PHP variable in href? - php

I made a FacebookConnect class in PHP for a project. It's working well, but the problem is that I'm unable to use the href correctly.
Connect with fb
I'm trying to call a method from one of my controllers in the href. I want to make it so that when I click on the button, it starts the login process.
public function _loginFacebook()
{
$connect = new FacebookConnect($appid,$app_secret);
$user = $connect->connect('http://localhost/washare/?site=public&module=Compte&action=profil');
if (is_string($user)) {
return $user;
}
}
In fact, I wanted to use the $user variable (which is a redirect URL) in the href directly, but iI guess I'm doing this wrong. Can someone help me?

Function _loginFacebook is called? If so, what part of code is achieved?
You can use header for redirecting on php.
<?php
header('Location: mapeamento.php');
?>

Related

Reload current URL in Codeigniter After Submit The Form

I have one form in my view.php file. Its URL like https://example.com/members/view/1, https://example.com/members/view/2 etc. When I submit form its calling model through controller like below
public function insert_comments(){
$data=$this->input->post();
$this->load->model('work_model');
$result=$this->work_model->insert_comments($data);
if($result)
{
$this->session->set_flashdata('insert_comments','your comments succesfully');
$this->session->set_flashdata('succesfully','alert-success');
$this->load->view('add_coments');
}
else{
$this->session->set_flashdata('insert_comments','your comments failed');
$this->session->set_flashdata('succesfully','alert-danger');
$this->load->view('add_coments');
}
}
}
and model is like below
public function insert_comments($array)
{
return $this->db->insert('comments',$array);
}
Currently its working fine and on form submit its loding view called add_comments, instead I want reload/refresh current page. I am not able to get idea of how I can do it, let me know if someone can help me for do it.
Thanks!
According to this answer, in the controller you can use:
redirect($this->uri->uri_string());
use this :
redirect($_SERVER['REQUEST_URI'], 'refresh');
Whenever you are reloading or redirecting a page you should always use the redirect() method instead of loading a view.
Redirection basically uses the header() method of core PHP and redirection will never execute the code blocks written beyond the redirect() method. But in the case of loading the view, it can execute until the end of the code block.
In your code, replace the line $this->load->view('add_coments'); with the redirection to the desired controller
redirect('your-controller','refresh');
I hope that helps you.

Codeigniter Controller return to previous page

I'd like to ask you how can I instead of $this->load->view('some_view.php') at the end of controller code, return user to page from where he invoked controller method? Simple return statement is not working.
ie.
public function someMethod($IDCustomer) {
$this->Some_modal->persist($IDCustomer);
// how to return to previous page instead of line after?
// i've used $this->load->view('someView.php');
}
This should help http://www.codeigniter.com/user_guide/libraries/user_agent.html
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
or straight PHP:
redirect($_SERVER['HTTP_REFERER']);
I've found answer on some thread.
In the page that you want to go back to you can do:
$this->session->set_userdata('referred_from', current_url());
Then redirect back to that page
$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');
Tried return redirect()->to($_SERVER['HTTP_REFERER']); , this would work well.
In Codeigniter 4 You can use previous_url() function from url helper
find more https://codeigniter.com/user_guide/helpers/url_helper.html
I've tried header('location:'.$_SERVER['HTTP_REFERER']); and it's working quite well.
Just a one-liner plain old PHP code.
Use the REDIRECT_QUERY_STRING alternative of HTTP_REFERRER:
// set in session redirect back URL in your common is_logged_in function if user is not logged in
$CI->session->set_userdata('redirect_back', $_SERVER['REDIRECT_QUERY_STRING']);
// below code user after successful login in auth.php library
if($this->ci->session->userdata('redirect_back')){
$redirectBackUrl = $this->ci->session->userdata('redirect_back');
$this->ci->session->unset_userdata('redirect_back');
redirect(base_url() . $redirectBackUrl);
}

Facebook getLoginUrl returning code variable in URL

I've been having a tough time trying to get the FB PHP SDK Login functionalities to work properly. The problem is with the login URL, which is not working for me. Upon clicking the link to log in, generated by `
//Generate FB log in and log out links
function FBLinks() {
global $FB;
if($this->GetFBID()) {
return 'https://www.facebook.com/logout.php?next='.BASEURL.'/logout.php&access_token='.$FB->getAccessToken();
} else {
return $FB->getLoginUrl($params = array('scope'=>'email'));
}
}`
, which generates a link like this one https://www.facebook.com/dialog/oauth?client_id=1525824323423421&redirect_uri=http%3A%2F%2Fmyurl.com%2F&state=b89f0cd22895184a2f0e2f0fe3155d39&scope=email
and upon clicking it, it takes me to: http://myurl.com/?code=AQBbMggtb5YxycOEB5BaBWzrqCuTJ6nWokOID4MvoyQ81BtIetAtzVe71dyXI9b6vvXdZ4fLHaMrumraQz2VeU2qcYFr9DbbZQ5ApRHVivUsv3L3h0UbV0X5A3eP34WvBhlNpWVpfm8kxUI8dv8s4XkmzydVIm3BDR71HGv65zV0w-FBEyg97-SLlFxP71iBeNvQCDqZRBVBkxEsjyxdlmqu7tIZGBl0ws4DI2wtrTuuf5DL4iVHq7NPOFomIyfCGt-73t24EbsfzLnq9ezi_U59V938oU9Zi3dEiiGHi7lFezPHT1oILe6CxaVR0Zecyb0&state=b89f0cd22895184a1f0e2f0fe3555d39#_=_
which shows nothing.
Is there something I need to do with the code variable?
Any help would be greatly appreciated. I read
facebook login returns code as variable in query string and seems like the OP had the same problem as me, but couldn't get to work his solution on my end.
Best Regards,
Richi
~~
Reading more about it, seems like I need to exchange that code for an access token somehow..
If you use the official facebook sdk, you don't have to care about the code param - this param is handeld from the sdk's php script.
You just have to use the getUser() function and check for a value($fbid) != 0
After that, you could query the fb api with wathever you want
$fbid = $this->facebook->getUser();
if ($fbid) {
$profile = $this->facebook->api("/me");
}
I figured out what was wrong and fixed it. The FB code was conflicting with my own code. Thanks for you time everyone.

How to pass keyword to landing page using PHP?

I'm using a landing page to redirect to my tracking url, how do I pass the keyword to my landing page which then passes it to my tracking link? Here is what my tracking link ends with:
&keyword={keyword}
I have tried doing http://www.foo.com?keyword={keyword} but no luck.
My landing page has this code:
<?php
header("Location: http://trackinglink.com&keyword={keyword}");
exit;
?>
Help please!!
edit: sorry for not being clear, I'm doing PPC and I need to use a landing page instead of direct linking (tracking url) so I need to be able to pass the keyword from the lander (which I'm gonna give to the PPC network) to my tracking link so I can see what keywords convert.
I need to know how to format the url to pass the keyword and what code to put into the landing page. thanks.
either the below, or I am not sure what you've asked at all
<?php
$keyword = 'location';
header("Location: http://foo.com?keyword=".$keyword);
exit;
?>
class saveKeys {
public function setKeyword($keyword) {
$this->keyword = $keyword;
}
public function getKeyword() {
return $this->keyword;
}
}
$keyword = new saveKeys();
$keyword->setKeyword($_GET['keyword']);
Now you saved the keyword in the setter and you can get it on the other page.

How to link a HTML link with a PHP function?

How to link a HTML link like this - Click here to log out
to a PHP function like - logout()
What I need to do is, when people click a link, php will run the php function.
Please advice! :D
What I need to do is, when people
click a link, php will run the php
function.
You can not call a PHP (server-side language) function when clicking on a link. From your question, I suspect you want to provide a link for users to logout, below is how you should go about.
Your link should be like:
Click here to log out
And in logout.php you should put php code for logging the user out.
Code inside logout.php might look like this:
<?php
session_start();
unset($_SESSION['user']); // remove individual session var
session_destroy();
header('location: login.php'); // redirct to certain page now
?>
There are a number of ways; just to get this out of the way first. There is no way to invoke a PHP function from the client side (i.e, user interaction at the browser) without a page refresh unless you use AJAX. As one answer suggests, you can put the function in a PHP page, and link to it. Here's another way
Logout
And inside index.php
<?php
switch $_GET['action']:
{
....
case 'logout': logout(); break;
...
}
?>
There are other more sophisicated methods, such as determining which function to call from URI directly, which is used by frameworks like CodeIgniter and Kohana.

Categories