aeboi80 Posted July 6, 2010 Share Posted July 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/ Share on other sites More sharing options...
Alex Posted July 6, 2010 Share Posted July 6, 2010 You don't need preg_replace for this because you don't require the use of regular expressions. Simply use str_replace. $str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str); Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081738 Share on other sites More sharing options...
aeboi80 Posted July 6, 2010 Author Share Posted July 6, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081742 Share on other sites More sharing options...
trq Posted July 6, 2010 Share Posted July 6, 2010 Where is $str defined? Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081743 Share on other sites More sharing options...
aeboi80 Posted July 6, 2010 Author Share Posted July 6, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081744 Share on other sites More sharing options...
trq Posted July 6, 2010 Share Posted July 6, 2010 It needs to be defined somewhere before this line.... $str = str_replace('%%%SPONSOR%%%', $_SESSION['sponsor'], $str); Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081750 Share on other sites More sharing options...
trq Posted July 6, 2010 Share Posted July 6, 2010 Your code still doesn't make allot of sense. Replacing %%%SPONSOR%%% and saving the result in $str still isn't going to magically replace %%%SPONSOR%%% within your tpl file if that's what your trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081751 Share on other sites More sharing options...
aeboi80 Posted July 6, 2010 Author Share Posted July 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081754 Share on other sites More sharing options...
Alex Posted July 6, 2010 Share Posted July 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206844-preg_replace-sponsor-need-help/#findComment-1081757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.