Jump to content

Recommended Posts

I am working on creating a login system with an affiliate referral feature.  Each member has a sponsor (or affiliate sponsor).  When a member is logged in I would like to use something like %%%SPONSOR%%% within the member files and have it display the logged in member Sponsor Name.

 

I realize that on my login.php file I could simply set

 

$_SESSION['sponsor'] = $row['sponsor'];

 

however i do not want to have to place

 

<?php echo $_SESSION['sponsor']; ?>

 

every time I want to display the logged in members sponsor.  I would rather just type in something like:

Your sponsor name is: %%%SPONSOR%%%

and replace it with their sponsor name.

 

I assume that using preg_replace is the correct function to be using for this, however I am not sure how to use it properly.  I have reviewed the instructions on php.net , but I can't figure out how to specify the actual value which %%%SPONSOR%%% will become after the string is identified.

 

Link to comment
https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/
Share on other sites

I keep getting an error:

 

Notice: Undefined variable: str in E:\Chad\Personal Projects\Project T\web\members\index.php on line 14

 

 

which on line 14 i have the following:

 

$str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str);

 

If i type in

<?php echo $_SESSION['sponsor'] ?>

it works so I know that the session variable is properly set.

 

 

$str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str);

Where is $str defined?

 

It is defined in the members/index.php file

 

<?php

require_once ("../inc/config.inc.php");

require_once ('../inc/cookie.php');

include_once (DOC_ROOT . "/inc/functions.php");

require_once (MEMBERS_TEMPLATES_PATH . "/member_header.php");

$str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str);

require_once (MEMBERS_TEMPLATES_PATH . "/member_index.tpl");

require_once (MEMBERS_TEMPLATES_PATH . "/member_footer.php");


?>

 

and then in the member_index.tpl file I have:

 

<h2>Members Area...</h2>

<p>Welcome, you are now logged in!</p>
<p>Your sponsor name is %%%SPONSOR%%% </p>
<p><a href="logout.php" title="Logout">Logout</a></p>

Yes, I realize that.  However, that is what I am trying to do.  That is why started this thread in the first place.  I want to have %%%%SPONSOR%%% and have it replace all instances of %%%SPONSOR%%% with the value stored within the sponsor field within the members table of the current user logged in.  What is the best way to go about this?  The first person who replied to my thread suggested the use of

[php[$str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str);[/code]

which I didn't see how it would parse the file and replace all instances of %%%SPONSOR%%%

 

Anyone have any other thoughts?

You can use output buffering and replace what you want before you actually output any content.

 

<?php
ob_start();

require_once ("../inc/config.inc.php");

require_once ('../inc/cookie.php');

include_once (DOC_ROOT . "/inc/functions.php");

require_once (MEMBERS_TEMPLATES_PATH . "/member_header.php");

require_once (MEMBERS_TEMPLATES_PATH . "/member_index.tpl");

require_once (MEMBERS_TEMPLATES_PATH . "/member_footer.php");

echo str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], ob_get_contents());
?>

 

ob_start

ob_get_contents

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.