Read Cookie PHPSESSID in opencart:2.3.0.2 - php

I want to use an opencart database for another website, so users don't need to register their details again.
For example: I sell Clothes (unStitched) on opencart and I also give services for stitching them. Opencart doesn't have a module to do this, so I want to redirect users to another website and there I want to read the user details for further processing.
How can I read PHPSESSID cookie? How does opencart read firstname on edit profile forms?
<?php echo $firstname; ?>
Thanks in advance.

In order to work with the Session data in an Opencart view, you will need to add some code to the controller. First we need to add an item in the $data array that the controller uses to pass variables to the view:
Assuming you want to show this link in a product view, you will need to edit the product controller's index action.
Controller file: public_html\catalog\controller\product\product.php
Find this line:
if ($product_info) {
Add the text inside the brace:
$data['mysessionvariable'] = $this->session;
You can restrict the available data to the sessionID only using the code below:
$data['mysessionvariable'] = $this->session->getId();
Now the relevant PHP Session data is available to the view, you can simply reference the session variable in the view wherever your need to. e.g. echo $mysessionvariable['session_id']; or echo $mysessionvariable;
View file: public_html\catalog\view\theme\default\template\product\product.tpl

Related

PHP: How to view product details in a separate page without having to create a new file for each product?

So I've created a market place page that lists all products from a database. They each have buttons to view their details (price, description, etc.) but I don't know how to let the user view the details without creating a new php page for each product.
I've tried using $_GET (for example, the url would read www.websitename.com/page.php?productname=name), but as I need to pass variable for prices as well, I can't use GET or a user could modify prices within the url. I've also tried using POST, but I would like the user to be able to bookmark or refresh the page without the variables clearing. I've tried setting $_SESSION variables for things like prices to avoid this issue, but as the session variables are still inevitably defined by POST or GET, I still run into both issues listed previously.
I've also tried using SQL queries so that the prices are received on demand from a database when the page (viewproduct.php) is loaded, and it searches for the product name that matches the $_GET['name'] variable, but the products are stored by category in different tables, and I can't check against all tables because they don't have equal dimensions.
So my question is simple: what is the standard for doing something like this? How can I write php code so that a user can bookmark or refresh a page with product information, without being able to edit any of the variables, and without me having to create a file for each and every product?
Thanks in advance!
Actually, you can link and change everything corresponding to your products. For example, you can link your images this way:
echo("<img src=../imgs/" . $_GET['product_name'] . " alt=". $_GET['product_name'] . "/>");
And display in the same way every information your have in your database such as comments corresponding to your product, presentation, price, reviews, ranking, etc...

Adding functionality to view in admin mode

I am developing a website, and I need to give 'admin' user CRUD(create/read/update/delete) functionality.
I am developing using php and CodeIgniter.
I have a view called gallery.php which display thumbnails, as grid.
each thumbnail have 2 buttons below(edit,delete) the thumbnail.
I want...
admin user - need to see those 2 buttons.
all rest wont see those 2 buttons.
I what to use the same view(not to duplicate it...) and just hide those buttons in case user is not admin.
What do I need to do? passing $is_admin to gallery.php seems to be a little ugly i guess.
Thx
when you login check its admin or not than store into session variable.
no need to create same page again & again ..
no need to pass variable into url..
just use session variable for checking its admin or not.

Menu and all pages content from database

I'm writing simple website with some cms functions. It would be good to have all menu items and pages contents held in database.
I have table with id, name, parent_id and a content field. In future I would maybe move content to a content table to have multiple contents to menu item with fk. But it is not the case here.
The question is:
Do I need the URL field in menu table?
What else do i need to get it to work? Should every page have its own controller? I,m a beginner with zend framework, so please give me some directives. Thanks in advance.
Lets start with Question 2: every page does not need its own controller. If your pages are static you can even load every page using a single action. For more dynamic processing you could use a separate action for each page.
In any case, make sure you structure your code into controllers and actions in a way that makes sense. For example, inside your CMS a user might edit, create or delete a post. You could then create a PostController inside which you write an editAction, createAction and deleteAction.
You could store the URL in the table, but you do not necessarily have to.
Single action approach (mostly for static content)
Make sure the page id or name is stored in a GET param. You could then use the following code:
public function genericpageAction()
{
$thePageID = $this->_request->getParam('id');
// fetch the page content from the db based on $thePageID
// and pass it to the view
}
Of course, here, you could also match against the URL stored in the table if you chose that approach.
Multiple action approach (for more dynamic processing, most likely what you want with a CMS)
You could define a route for each page and load its content in the respective action. For example, for the page to edit a post:
class MyCMS_PostController extends Zend_Controller_Action
{
public function editAction()
{
// fetch the home page content
// do any further processing if necessary
}
}

Joomla 1.5 Front End user add content into custom component

I built a custom component for Joomla 1.5. It' an FAQ component.
I'd like to let users add questions from the Front-End.
I have several fields that shouldn't be displayed for the user on the front end.
For ex. in the back-end admin has fields like Approved, Ordering and Published and the rest. I would like to let any user without signing in to add question front the front-end but these 3 fields shouldn't be displayed to the users on the front-end.
So, how to build the front-end user input?
Maybe someone has done that or know some good tutorial for this case?
In the view.html.php file of your component (eg. com_faq/views/view.html.php) you can define the mark up for your input field section. I build up a $html variable like:
$html .= '<input name="addQuestion" value="" type="Text"/>';
then add a reference to it:
$this->assignRef("addQuestion", $html);
so that in your view template (i.e. com_faq/views/tmpl/default.php) you can add it to your page like
echo $this->addQuestion;
When you click your submit button you can reroute back to the same view. So user a url like
index.php?option=com_faq&task=addQuestion&view=default
So before you mark up your page (so within the first few lines of your display function for example) you can grab the contents of your user's input on the front end
$question = JRequest::getVar('addRequest', null);
Once you have this you can either store it to your database or display it. Alternatively you can AJAX submit your form and process it in a controller function so the you don't have the refresh etc.
You will need to edit your router.php file to pick up the task and pass it to your controller i.e. set it as a task or a view.
There are loads of options for this but fundamentally there are 3 things you need:
Create your mark up in your view.html.php file and assign a reference to it
Include the reference in your template i.e. default.php
Submit your form to an address that your same component can process it i.e. index.php?option=com_faq&task=addQuestion&view=default
Hope this helps :)

Accessing user profile variables

Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?
I used the dsm($user) function to output the user variables and it contained everything except the data for the profile_real_name
I also ran dsm($vars) on the phptemplate_preprocess_user_profile and I could see it contained an object called account which did contain the info I needed but this object isn't available in the $user variable.
Many thanks
If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:
This goes in the phptemplate_preprocess_node function in your template.php file:
if ($vars['uid']) {
$vars['account'] = user_load(array('uid' => $vars['uid']));
}
Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:
print($account->profile_real_name);
However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?
If so, a MUCH more efficient way would be to override the theme_username function.
That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6:
http://drupal.org/node/122303#comment-204277
$account is what you usually call a user that isn't the global user to avoid accidently overwriting the global user which would result in the user be get logged in as that user.
I just did a bit of checking and the easiest way to solve your problem is to use $account in the template instead of $user.
Using $user in the template or doing like WmasterJ suggests is faulty. You will post the wrong data. You will post the data of the logged in user not the data of the user who's profile is being watched. This bug will happen when you view all other users' profile than your own.
Preprocess functions is not hard to make, in your template.php file in your theme you just replace phptemplate with your theme's name defined the code. In this case you wont need to alter the preprocess function, since you already have what you need.
If you want to do this within for instance the user-profile.tpl.php all the information you need exists within the $account array.
Otherwise you can access user data by loading a user object based on it's id (of the currently logged in person that is, or if you can query the DB and get uid that way).
First get the uid of the current user:
$uid = $user->uid;
Then load the a user object:
// Create user objets based on uid ()
$user_obj = user_load($user->uid);
Then load that users profile variables:
// Load profile
profile_load_profile($user_obj);
Now the $user_obj variable (which is passed by reference to profile_load_profile) has an object with the the profile information that can be accessed like this:
$user_obj->profile_real_name
Hope it helps!

Categories